Ich entwickle auf ASP.NET MVC4. Ich habe zwei JSON-Anforderungen in meinem Code, die ein JSON-Objekt übergeben. Einer von ihnen funktioniert gut, der andere besteht aus irgendeinem Grund eine Null. Irgendwelche Ideen?Ähnliche JSON-Anfragen, aber eins sendet Nullobjekt
Hinweis: In beiden Fällen erreicht die Anforderung tatsächlich den gewünschten Controller. Es ist nur so, dass die zweite eine NULL statt meines gut bevölkerten Objekts übergibt.
Arbeits 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*/);
}
});
});
Arbeits Controller Aktion:
[Authorize(Roles = "edit,admin")]
public ActionResult AddToInvoice(InvoiceItem item)
{
return Json(item);
}
JavaScript, das ein Null-Objekt übergibt:
$('#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);
}
});
});
Controller-Aktion, die ein Null-Objekt erhält:
[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; }
}
Route Werte:
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 zeigt ein Element JSON Objekt ist, wie erwartet. Auch ein "stringified" obj. Das Debuggen von serverseitigem Code zeigt, dass der myCredential-Parameter null ist.
wo ist InvoiceItem und myCredential? Auch Ihre Routen-Definition. – roryf
Erhält die serverseite wirklich nichts? Was wird in Firebug gesendet? Was sagen deine Routenwerte? – iappwebdev