Sto sviluppando su ASP.NET MVC4. Ho due richieste JSON nel mio codice che invia un oggetto JSON. Uno di loro funziona bene, l'altro passa un nulla per qualche motivo. Qualche idea?Richieste JSON simili ma una invia oggetto nullo
Nota: in entrambi i casi, la richiesta raggiunge effettivamente il controller desiderato. È solo che il secondo passa un NULL, invece del mio oggetto ben popolato.
JavaScript lavoro:
$('#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*/);
}
});
});
lavoro azione di controllo:
[Authorize(Roles = "edit,admin")]
public ActionResult AddToInvoice(InvoiceItem item)
{
return Json(item);
}
javascript che passa un oggetto 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);
}
});
});
azione di controllo che riceve un oggetto nullo:
[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; }
}
rotta valori:
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 mostra elemento è un oggetto JSON, come previsto. Anche un obj "stringificato". Il debug del codice lato server mostra che il parametro myCredential è nullo.
dove è InvoiceItem e myCredential? Anche la tua definizione di rotte. – roryf
Il serveride non riceve davvero nulla? Cosa viene inviato in firebug? Cosa dicono i valori del tuo itinerario? – iappwebdev