Reputation: 472
Can someone please educate me on why I cannot have a JSON property with the same name as an MVC controller parameter? I'm sure I'm making a dumb mistake somewhere, but here is the scenario:
I commit an $http.post to an MVC controller
$http.post method:
return $http.post("/Api/Form/Create/Save/DraftForm/", {"Model":"test"});
MVC Model
public class FormViewModel
{
public string Model { get; set; }
}
MVC Controller
[HttpPost, Route("Api/Form/Create/Save/DraftForm")
public ActionResult Create_Save_DraftForm(FormViewModel model)
{
....
}
By the time someObj gets to the controller, it is null.
However, if I change the name of the parameter in the controller "model" to something like "viewModel", it works fine.
[HttpPost, Route("Api/Form/Create/Save/DraftForm")
public ActionResult Create_Save_DraftForm(FormViewModel viewModel)
{
....
}
As Shyju mentioned, I can also rename the "model" property in the JSON object to another name and it works as well:
public class FormViewModel
{
public string AnotherModel { get; set; }
}
return $http.post("/Api/Form/Create/Save/DraftForm/", {"AnotherModel":"test"});
Upvotes: 0
Views: 134
Reputation: 472
I misunderstood how data was getting posted to the controller.
The controller is getting
{"Model", "test"},
but is expecting
{ "model":
{"Model", "test"}
}
Basically, the data being passed to the controller should have a property name that matches the controller parameter, and the property should be an object that matches the parameter's object in the controller.
Upvotes: 0