1

i am trying to get the value of selected check-box using model but not able to get as i want ;

Below is the table image enter image description here

below is my code for this VIEW

enter image description here

And below is the code for result.I get null value

enter image description here

And below is my model declaration

public class RoleDetail
  {
    [Key]
    public int RoleDetailID { get; set; }

    public bool IsCreate { get; set; }
    public bool IsDelete { get; set; }
    public bool IsView { get; set; }
    public bool IsEdit { get; set; }
    public bool IsDownload { get; set; }
    public string ControllerName { get; set; }
    public System.DateTime CreateDate { get; set; }
    public Int32 UserTypeId { get; set; }
}
public enum ControllerName
{
    Account, Candidate, Career, ChooseUs, ContactUs, DocumentType, Employee, Gallery, GalleryType, GetTouch, Home, JobCategory, Jobs, Portfolio, ResumeUpload, RoleDetail, Services, User, UserRoleType

}
hii
  • 255
  • 4
  • 18

2 Answers2

2

Replace the foreach loop in your view with a for:

@for (var i = 0; i < lst.Count; i++)
{
    ...
    @Html.CheckBoxFor(x => lst[i].IsCreate)
    @Html.CheckBoxFor(x => lst[i].IsView)
    @Html.CheckBoxFor(x => lst[i].IsDelete)
    ...
}

For this to work make sure that the variable you are iterating over is an IList<T> or T[].

Also your controller action argument should be named accordingly:

public ActionResult Create(IEnumerable<RoleDetail> lst)
{
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • ControllerName(it's variable) isComing from from different model.That's why i need to create a RoleDetail object in view .Please check the view Image in the question – hii Jan 26 '15 at 17:06
0

You should not be creating RoleDetail in the view. In the GET method create a List<RoleDetail>, populate it with the objects you want to display and return it to the view.

Controller

public ActionResult Create()
{
  List<RoleDetail> model = new List<RoleDetail>();
  // populate the collection, for example
  foreach(var name in Enum.GetNames(typeof(ControllerName)))
  {
    model.Add(new RoleDetail()
    {
      ControllerName = name,
      IsCreate = true // etc
    });
  }  
  return View(model);
}

public ActionResult Create(IEnumerable<RoleDetail> model)
{
}

View

@model List<RoleDetail>
@using(Html.BeginForm())
{
  for(int i = 0; i < Model.Count; i++)
  {
    @Html.HiddenFor(m => m.ControllerName) // needed for postback
    @Html.DisplayFor( => m.ControllerName)
    @Html.CheckBoxFor(m => m.IsCreate)
    ....
  }
  <input type="submit" />
}

Side notes

  1. Do not try to override the name (or value) attribute. The html helper set these correctly for model binding (and in any case you were only setting it to the value the helper generated anyway)
  2. The reason the foreach loop does not work is your generating duplicate name attributes (and also invalid html due to duplcate id attributes). The for loop correctly generates the correct names with indexers (e.g. <input name="[0].IsCreate " ..>, <input name="[1].IsCreate " ..> etc.
  3. You don't appear to be rendering controls for all of you model properties so use a view model containing only those properties you need to display/edit. What is a view model in MVC
  4. You have public enum ControllerName so I suspect property ControllerName in RoleDetail should be public ControllerName ControllerName { get; set; }?

And in future, post the code, not an image of it!

Community
  • 1
  • 1