views:

50

answers:

1

Hi, 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=vm.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

+2  A: 

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() {}
}
Jab
Well, I dont have a constructor in it..I am using the default constructor
Misnomer
Can we see the ViewModel's code?
kmehta
I have posted the viewmodel above now...and actually like on the form where i am using a dropdown list to get the values of the someclass object that's the problem...I mean i am creating a dropdown list on the Get method and using Model.Dropdownlist on the form and that's where it goes wrong...if i comment that...it goes in the method with parameters..but i dont understand since i have a default constructor.
Misnomer
@vj SomeClass also has to have a parameterless constructor, too.
Will
Ya I fixed it actually it was a list that I was creating I have added it to the viewmodel...and i dont create a new one on the get method...so now it works!!..thx for your help !!
Misnomer