Tôi đang phát triển trên ASP.NET MVC4. Tôi có hai yêu cầu JSON trong mã của tôi gửi một đối tượng JSON. Một trong số họ hoạt động tốt, người kia vượt qua một null vì lý do nào đó. Bất kỳ ý tưởng?Yêu cầu JSON tương tự nhưng một gửi đối tượng null
Lưu ý: trong cả hai trường hợp, yêu cầu trên thực tế đến được bộ điều khiển dự định. Nó chỉ là thứ hai vượt qua một NULL, thay vì đối tượng dân cư độc đáo của tôi.
javascript làm việc:
$('#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*/);
}
});
});
điều khiển hành động làm việc:
[Authorize(Roles = "edit,admin")]
public ActionResult AddToInvoice(InvoiceItem item)
{
return Json(item);
}
javascript mà vượt qua một đối tượng NULL:
$('#btnApplyDiscount').click(function() {
var item = { user: $('#txtAdminUser').val(),password: $('#txtPassword').val(), isvalid: false };
var obj = JSON.stringify(item);
alert(obj);
$.ajax({
type: "POST",
url: "<%:Url.Action("IsUserAdmin","Financials")%>",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: obj,
success: function (result) {
if (result.isvalid)
{
//do stuff
}
else
{
alert("invalid credentials.");
}
},
error: function (error) {
//do not add to cart
alert("Error while verifying user." + error.responseText);
}
});
});
điều khiển hành động tiếp nhận một đối tượng null:
[Authorize(Roles = "edit,admin")]
public ActionResult IsUserAdmin(myCredential user)
{
//validate our user
var usercount = (/*some LINQ happening here*/).Count();
user.isvalid = (usercount>0) ? true : false;
return Json(user);
}
UPDATE: InvoiceItem
public partial class InvoiceItem
{
public Guid? id { get; set; }
public string InvCatCode { get; set; }
public string Description { get; set; }
public decimal Amount { get; set; }
public decimal VAT { get; set; }
public int Qty { get; set; }
public decimal Rate { get; set; }
public Nullable<decimal> DiscountAmount { get; set; }
public string DiscountComment { get; set; }
public Nullable<bool> IsNextFinYear { get; set; }
public Nullable<System.DateTime> ApplicableFinYear { get; set; }
}
myCredential:
public partial class myCredential
{
public string user { get; set; }
public string password { get; set; }
public bool? isvalid { get; set; }
}
đường giá trị:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Firebug cho thấy mục là một đối tượng JSON, như mong đợi. Cũng là một obj "được xâu chuỗi". Gỡ lỗi mã phía máy chủ cho thấy tham số myCredential là null.
đâu InvoiceItem và myCredential? Ngoài ra định nghĩa tuyến đường của bạn. – roryf
Máy chủ thực sự có nhận được gì không? Những gì được sended trong firebug? Giá trị tuyến đường của bạn nói gì? – iappwebdev