I am trying to post back some data using a viewmodel i have created and it works for me for one of the projects.But I am doin this right now
public ActionResult Foo(string userkey)
{
vm.Value="Xvalue";
return View(vm);
}
[HttpPost]
public ActionResult Foo( MyViewModel vm)
{
// process input
if (inputOK)
string value=v开发者_如何学Pythonm.Value
return RedirectToAction("Index");
return View();
}
public class MyViewModel
{
public string Value { get; set; }
public SomeClass newobj {get;set;}
}
public class SomeClass
{
public int id{get;set;}
public string str{get;set;}
}
So it on debugging never goes into the parameter method for Post although on the view i have added a form and a button that submits and the page inherits from the viewmodel.I get an error saying it expects a parameterless constructor how do I fix this ? . I wrote an post method with no parameters and it does go into that method
The error you are seeing is likely happening in the ModelBinder. The Parameterless constructor is expected on the MyViewModel
class, not the Action. Make sure your MyViewModel
has a parameterless constructor.
public class MyViewModel
{
// stuff
public MyViewModel() {}
}
精彩评论