Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Simple model binding not working

Okay, this has got to be something stupid-as-a-box-of-rocks that I'm doing wrong, but I can't find it.

MVC Action:

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Create(BatchCreateViewModel createModel)
{
    return RedirectToRoute(MVC.Home.Display());
}

BatchCreateViewModel:

public class BatchCreateViewModel
{
    bool searchAVM;
    bool searchBPO;
    bool searchAppraisal;
    int transactionAge;
    string Description;
    string uploadfile;
}

There are controls on the View page named "searchAVM", "searchBPO", "searchAppraisal", (checkboxes) "transactionAge"(a set of radio buttons with integer values) and "description" (a text box)

When I break at the entry to "Create", createModel is there, but has all default values(null for the strings, false for the booleans, 0 for the int). If I examine Request.Form, the values are there, but they are just not getting into the model.

What am I doing wrong?

(This is under MVC 2, Framework 4.)

like image 902
Dave Hanna Avatar asked Sep 10 '10 14:09

Dave Hanna


1 Answers

Your view model should have automatic properties, not public variables. It's caught me out before!

So it should be:

public class BatchCreateViewModel 
{ 
    public bool searchAVM {get;set;}
    public bool searchBPO {get;set;}
    public bool searchAppraisal {get;set;}
    public int transactionAge {get;set;}
    public string Description {get;set;}
    public string uploadfile {get;set;}
} 
like image 99
Paul Hadfield Avatar answered Sep 25 '22 12:09

Paul Hadfield