我正在开发ASP.NET MVC4.我的代码中有两个提交JSON对象的JSON请求.其中一个工作正常,另一个由于某种原因传递null.有任何想法吗?
注意:在两个实例中,请求实际上都到达了预期的控制器.只是第二个传递NULL,而不是我填充的对象.
工作javascript:
$('#btnAdd').click(function () {
var item = {
Qty: $('#txtQty').val(),
Rate: $('#txtRate').val(),
VAT: $('#txtVat').val()
};
var obj = JSON.stringify(item);
$.ajax({
type: "POST",
url: "<%:Url.Action("AddToInvoice","Financials")%>",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: obj,
success: function (result) {
alert(result);
},
error: function (error) {
//do not add to cart
alert("There was an error while adding the item to the invoice."/* + error.responseText*/);
}
});
});
Run Code Online (Sandbox Code Playgroud)
工作控制器动作:
[Authorize(Roles = "edit,admin")]
public ActionResult AddToInvoice(InvoiceItem item)
{
return Json(item);
}
Run Code Online (Sandbox Code Playgroud)
传递NULL对象的javascript:
$('#btnApplyDiscount').click(function () …Run Code Online (Sandbox Code Playgroud) 我是MVC的新手.我在mvc中看到人们用这种方式生成html并绑定数据
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => p.Name, "Name:")
@Html.TextBoxFor(m => p.Name)
</div>
<input type="submit" value="Create" />
}
Run Code Online (Sandbox Code Playgroud)
但如果我不想使用html帮助扩展,那么我应该如何编码将正常的html控件和表单与模型数据绑定.请附上示例代码.谢谢