I’m working with Visual Studio 2010 and ASP.NET MVC4 view engine razor.
I like to pass two parameters (object, string) from view to controller.
In the view, i have this:
var persona = {};
function ModeloPersona() {
persona.CI = $('#CI').val();
persona.Nombre = $('#Nombre').val();
persona.Apellidop = $('#Apellidop').val();
persona.Apellidom = $('#Apellidom').val();
persona.Direccion = $('#Direccion').val();
persona.Sexo = $('#Sexo').val();
persona.Cumple = $('#Cumple').val();
}
function Grabar(modo) {
ModeloPersona();
$.ajax({
url: '@Url.Action("Grabar", "Home")',
type: 'POST',
data: {
Persona: JSON.stringify(persona),
Modo: modo
},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {
}
});
}
and my controller I have this:
public ActionResult Grabar(Persona mPersona, string modo){
if (ModelState.IsValid){
if (modo == "2"){
}
else{
}
}
return View();
}
the problem is the following, the object mPersona is null but value of modo is correct.
Why mPersona is null? what is the problem, please help me with this
Regards
Ricardo
Changing the name of the parameter to
personashould work:The names of the parameters to the action method have to match the names of the objects in the JSON data for the model binding to work.