You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将HTML表单输入存入JS对象时遇语法错误求解决(.NET MVC5)

Fixing the "Expected : Comma expected" Error & Implementing the AJAX Submit

First, let's break down why you're seeing that syntax error:

You've placed raw assignment statements directly inside the configuration object passed to $("#newAuction").validate(). Object literals (like the one you're passing to validate()) can only contain key-value pairs (e.g., submitHandler: function() { ... }), not standalone code like vm.auction.Title = .... That's what's causing the parser to throw a "comma expected" error—it's expecting another key-value pair, not arbitrary code.

Here's the corrected implementation that fixes the error and meets your requirements:

Corrected JavaScript Code

<script>
    $(document).ready(function() {
        // Initialize your view model
        var vm = {
            auction: {},
            productIds: []
        };

        // Set up form validation
        var validator = $("#newAuction").validate({
            submitHandler: function(form) {
                // Capture form values into the view model (do this on submit, not initialization)
                vm.auction.Title = $("#title").val();
                vm.auction.MinBid = parseFloat($("#minbid").val()); // Parse to numeric type
                vm.auction.EDate = $("#edate").val();
                vm.productIds = [1, 2, 3]; // Replace with dynamic logic if needed

                // Submit via AJAX with proper JSON formatting
                $.ajax({
                    url: "/api/newAuction",
                    method: "POST",
                    contentType: "application/json", // Notify API we're sending JSON
                    data: JSON.stringify(vm) // Convert object to valid JSON string
                })
                .done(function() {
                    toastr.success("Auction Added to the db");

                    // Reset the view model (modify existing object, avoid new variable scope)
                    vm.auction = {};
                    vm.productIds = [];

                    // Reset the form to clear inputs
                    validator.resetForm();
                })
                .fail(function() {
                    toastr.error("Something went wrong");
                });

                return false; // Prevent default form submission behavior
            }
        });
    });
</script>

Key Fixes & Improvements:

  • Resolved syntax error: All value assignment logic is now inside the submitHandler function, not directly in the validator's configuration object.
  • Captures latest input values: Values are collected when the user submits the form, not on page load (which would have grabbed empty initial values).
  • Proper JSON serialization: Added JSON.stringify(vm) and contentType: "application/json" to ensure your API receives the exact JSON structure it expects (most APIs require this instead of form-encoded data).
  • Numeric type handling: Converted MinBid from a string to a float so the API gets a numeric value instead of text.
  • Fixed view model reset: Instead of declaring a new local vm variable in the success callback, we reset the properties of the existing vm object to avoid scope confusion.

Notes:

  • If your productIds need to be populated dynamically (e.g., from selected checkboxes), replace the static [1,2,3] with logic like:
    vm.productIds = $(".product-checkbox:checked").map(function() {
        return parseInt($(this).val());
    }).get();
    
  • Ensure your API endpoint /api/newAuction is configured to accept POST requests with the JSON structure you're sending.

内容的提问来源于stack exchange,提问作者hkjhadj1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:15:19