FormPanel + Checkbox | MVC | Model Binder in Controller

  1. #1

    [SOLVED] FormPanel + Checkbox | MVC | Model Binder in Controller

    Consider these classes:

    // 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
  2. #2
    Ok, i got it... if anyone needs it, this is how you setup the CheckBox in the View:

     @Html.X().Checkbox()
        .Name("Valid")
        .Checked(Model.Valid ?? false)
        .InputValue("true")

    It was missing the InputValue property.

    And, if you want to receive the value, even if CheckBox is unchecked

     @Html.X().Checkbox()
        .Name("Valid")
        .Checked(Model.Valid ?? false)
        .InputValue("true")
        .UncheckedValue("false")
    Last edited by carlosveber; Oct 05, 2012 at 2:05 PM.

Similar Threads

  1. Replies: 0
    Last Post: Aug 01, 2012, 10:43 PM
  2. binding checkbox selection model
    By VipulTyagi in forum 1.x Help
    Replies: 2
    Last Post: Apr 07, 2011, 11:36 AM
  3. [CLOSED] checkbox selection model
    By majestic in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Nov 24, 2010, 1:22 PM
  4. Replies: 4
    Last Post: Oct 06, 2010, 9:08 AM
  5. Checkbox selection model + getRowsValues
    By Birgit in forum 1.x Help
    Replies: 10
    Last Post: Sep 06, 2010, 5:12 PM

Tags for this Thread

Posting Permissions