Say I have a default model which is posted via AJAX:
var data = {
// some members...
newMember: true
}
$.post("/mycontroller/action", data, function() {});
In my controller, I don’t want to pollute the existing model coming into the controller. I’d like to be able to add the newMember parameter to the action.
[HttpPost]
public ActionResult AddMember(MemberModel model, bool newMember)
{
}
Rather than change MemberModel, I’d like to have the model binder fill newMember, but this doesn’t work. How do I get round this problem?
That’s exactly what view models are intended for. Your controller actions must receive view models as arguments and pass view models to views.
But anyway if for some reason you don’t use view models in your application you could send a JSON request:
Bear in mind that this works in ASP.NET MVC 3. If you are using an older version you could write a custom JsonValueProvider factory as explained in this blog post.