Oct 05, 2012, 1:16 PM
[SOLVED] FormPanel + Checkbox | MVC | Model Binder in Controller
Consider these classes:
but CHECKBOX is not..
I can read the post values in a FormCollection object, but i have a form with too many Checkboxes, that would be fairly 'ugly' to code and mantain.
I have tryed rendering HTML inputs, but that does not work because there is no FORM tag in the HTML.
Are there any alternatives other than getting the values from FormCollection?
// Master
public class MasterModel
{
public MasterModel()
{
MyList = new List<DetailModel>();
}
public decimal? Id { get; set; }
public string Name { get; set; }
public bool? Valid { get; set; }
public List<DetailModel> MyList { get; set; }
}
// Detail
public class DetailModel
{
public decimal? ItemId { get; set; }
public decimal? Description { get; set; }
}
If I create a FormPanel with fields that have the same name of my model properties, the mvc engine will already map them on submit to an object, so i can retrieve in my controller method:using System.Web.Mvc;
public class MasterController : Controller
{
[HttpPost]
public ActionResult Item(MasterModel model)
{
// model.Id : is mapped to a TextField().Name("Id") << OK
// model.Name : is mapped to a TextField().Name("Name") << OK
// model.Valid : is mapped to a CheckBox().Name("Valid") << NOT OK !!!
// model.MyList[0].ItemId : is mapped to a TextField().Name("MyList[0].Name") << OK
// model.MyList[0].Description : is mapped to a TextField().Name("MyList[0].Description") << OK
// model.MyList[1].ItemId : is mapped to a TextField().Name("MyList[1].Name") << OK
// model.MyList[1].Description : is mapped to a TextField().Name("MyList[1].Description") << OK
return null; // just a sample
}
}
If i use TextFields or Hidden, the MVC Model Binder will work just fine..but CHECKBOX is not..
I can read the post values in a FormCollection object, but i have a form with too many Checkboxes, that would be fairly 'ugly' to code and mantain.
I have tryed rendering HTML inputs, but that does not work because there is no FORM tag in the HTML.
Are there any alternatives other than getting the values from FormCollection?
Last edited by carlosveber; Oct 05, 2012 at 1:37 PM.
Reason: solved