将 JSON 对象传递给控制器

Passing a JSON object to a controller

我是 MVC 和 JavaScript 的新手,虽然这对某些人来说可能是一个明显的答案,但我现在已经苦苦挣扎了一段时间(在查看了许多示例之后)!

通过 Index.cshtml 视图进行调试时,newData 对象尚未被文本框填充(在名为 AddNewProduct.cshtml 的部分视图中设置)。单击 Index.cshtml 中设置的按钮可显示部分视图(这很好用)。我可以看到由于视图中的问题,我的控制器中的 newProduct 对象尚未填充。

我的主要问题是如何将文本框中的值输入到 newData 对象中?

如有任何建议,我们将不胜感激!谢谢。

HomeController.cs:

[HttpPost]
        public PartialViewResult RunAddNewProduct(Product newProduct)
        {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = Connections.connection;
                con.Open();

                using (con)
                {
                    SqlCommand cmd = new SqlCommand("INSERT INTO Product VALUES(@Id, @Name, @Description, @Price, @UnitsInStock)", con);

                    cmd.Parameters.Add(new SqlParameter("@Id", newProduct.Id));
                    cmd.Parameters.Add(new SqlParameter("@Name", newProduct.Name));
                    cmd.Parameters.Add(new SqlParameter("@Description", newProduct.Description));
                    cmd.Parameters.Add(new SqlParameter("@Price", newProduct.Price));
                    cmd.Parameters.Add(new SqlParameter("@UnitsInStock", newProduct.UnitsInStock));

                    SqlDataReader rd = cmd.ExecuteReader();

                    while (rd.Read())
                    {
                        newProduct.Id = Convert.ToInt32(rd.GetInt32(0));
                        newProduct.Name = Convert.ToString(rd.GetSqlValue(1));
                        newProduct.Description = Convert.ToString(rd.GetSqlValue(2));
                        newProduct.Price = Convert.ToDecimal(rd.GetDecimal(3));
                        newProduct.UnitsInStock = Convert.ToInt32(rd.GetInt32(4));
                    }
                }
                return PartialView("AddNewProduct", newProduct);           
        } 

Index.cshtml:

$('#btnConfirmNewProduct').live('click', function () {

        var newData = {
                'id': $('#txtId').val(),
                'name': $('#txtName').val(),
                'description': $('#txtDesc').val(),
                'price': $('#txtPrice').val(),
                'unitsInStock': $('#txtUnitsInStock').val()
        };

        $.ajax({
            url: '/Home/RunAddNewProduct',
            data: JSON.stringify(newData),
            type: 'POST',
            dataType: 'json'
        })
            .success(function (result) {
                $('#products').html(result);
            })
            .error(function (xhr, status) {
                alert(status);
            })
    });

AddNewProduct.cshtml:

<table>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Description</th>
            <th>Price</th>
            <th>Units In Stock</th>
        </tr>
        <tr>
            <td><input id="txtAddId" type="text"/></td>
            <td><input id="txtAddName" type="text"/></td>
            <td><input id="txtAddDesc" type="text"/></td>
            <td><input id="txtAddPrice" type="text"/></td>
            <td><input id="txtAddUnitsOfStock" type="text"/></td>
        </tr>
    </table>
    <input id="btnConfirmNewProduct" type="button" value="Confirm New Product" />

替换

data: JSON.stringify(newData),

data: newData,