1

I am new in ASP.MVC4 and I am facing with problem to pass object from view to controller: Let me explain my problem from beginning: My class which is used by is for example: UserAndRolesModel

public class UserAndRolesModel
{
    public user User { get; set; }
    public IEnumerable<UserAndRoles> AsignedRoles { get; set; }
}

As You can see class UserAndRolesModel persists of 2 objects: user and Enumerable

public class user
{
    [HiddenInput(DisplayValue=false)]
    public virtual int user_id { get; set; }

    [Required(ErrorMessage = "Please provide name")]
    [Display(Name = "Name")]
    public virtual string user_name { get; set; }

    [Display(Name = "Is active?")]
    public virtual bool user_active { get; set; }

}

public class UserAndRoles
{
    public string RoleName { get; set; }
    public bool IsAssigned { get; set; }
}

My controller action is simple, it assign user to UserWithRoles.User and creates tempList with UserAndRoles objects.

    public ActionResult Edit(int userid=0)
    {
      //object which will be passed to View
      UserAndRolesModel UserWithRoles = new UserAndRolesModel();

      //user
      UserWithRoles.User = repoUsers.Users.FirstOrDefault(x => x.user_id == userid);

      //roles
      IList<UserAndRoles> tempList = new List<UserAndRoles>();
      UserAndRoles temp1 = new UserAndRoles();
      temp1.RoleName="Admin";
      temp1.IsAssigned=true;
      UserAndRoles temp2 = new UserAndRoles();
      temp2.RoleName="User";
      temp2.IsAssigned=false;
      tempList.Add(temp1);
      tempList.Add(temp2);

      //assign tempList to model
      UserWithRoles.AsignedRoles = tempList;

      return View(UserWithRoles);
    )

At this stage I am successfully passing to View:

UserWithRoles.User.user_id=1;
UserWithRoles.User.user_name="UserName1";
UserWithRoles.User.user_active=true;
UserWithRoles.AsignedRoles[1].RoleName = "Admin";
UserWithRoles.AsignedRoles[1].IsAssigned = true ;
UserWithRoles.AsignedRoles[2].RoleName = "User";
UserWithRoles.AsignedRoles[2].IsAssigned = false;

I am able to display above View properly:

@model Models.UserAndRolesModel
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <div class="editor-field">
            @Html.EditorFor(model => model.User.user_id)
            @Html.ValidationMessageFor(model => model.User.user_id)
        </div>


        <div class="editor-label">
            @Html.LabelFor(model => model.User.user_name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.User.user_name)
            @Html.ValidationMessageFor(model => model.User.user_name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.User.user_active)
        </div>
        <div class="editor-field">
            @Html.CheckBoxFor(model => model.User.user_active)
            @Html.ValidationMessageFor(model => model.User.user_active)
        </div>

        <br /> ROLES below is piece of code which makes me cry<br />
        @foreach (var item in Model.AsignedRoles)
        {
            <div class="editor-field">
            <div class="editor-field">
               @Html.LabelFor(model => item.RoleName)
               @Html.CheckBoxFor(model => item.IsAssigned)
            </div>
            </div>
        }

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

When i Click submit all data regarding user was passed properly but IEnumerable AsignedRoles is always null. Here is my controller method on post:

[HttpPost]
public ActionResult Edit(UserAndRolesModel UserWithRoles)
    {
        if (ModelState.IsValid)
        {
                        if (UserWithRoles.AsignedRoles==null)
                            Console.WriteLine("I am still crying");
                        else
                            Console.WriteLine("Got it!");
        }
        return View(UserWithRoles);
    }

In View I tried to use other loops for example:

    @for (int i = 0; i < Model.AsignedRoles.Count(); i++)
    {
             <div class="editor-field">
               @Html.LabelFor(model => item[i].IsAssigned)
               @Html.CheckBoxFor(model => item[i].IsAssigned)
             </div>
    }

But above also does not pass IEnumerable. Can anyone help me to resolve this issue? How can I pass back to controller UserAndRolesModelwhich contains IEnumerable? I will be very grateful. Advance thanks for the help!

JacekRobak85
  • 93
  • 1
  • 10
  • Can you use Fiddler to see what is actually being POSTed back? – dav_i Nov 04 '14 at 12:53
  • 1
    did you try to use `IList` instead of `IEnumerable`? Also, because I can't bear to not comment on it; `AsignedRoles` is missing an s, and `UserWithRoles` does not follow your convention of lowerCasedVariableNames. – AlexanderBrevig Nov 04 '14 at 12:59
  • 1
    ^never mind the IList, using a for loop is what you need. You should also check you html source to see that it is generating something like `[0].IsAssigned [...] [1].IsAssigned`. – AlexanderBrevig Nov 04 '14 at 13:05
  • The task of binding a list is well [documented](http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/), this question has also been asked countless times on the site already. – James Nov 04 '14 at 13:12
  • Thanks AlexanderBrevig, the problem was in IEnumerable and IList. I had to change it to IList and now its works perfectly. – JacekRobak85 Nov 04 '14 at 13:24

1 Answers1

1

You do need the for loop, but the one you tried you have referenced item[i], yet item[i] no longer exists. Try this, note that I have also added a HiddenFor for RoleName otherwise that won't get passed back:

@for (int i = 0; i < Model.AsignedRoles.Count(); i++)
{
         <div class="editor-field">
           @Html.LabelFor(model => model.AssignedRoles[i].IsAssigned)
           @Html.CheckBoxFor(model => model.AssignedRoles[i].IsAssigned)
           @Html.HiddenFor(model => model.AssignedRoles[i].RoleName)
         </div>
}
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Thanks mattytommo for answer! The problem was in IEnumerable and IList but Your piece of code helps me to get it clear. Thank You Guys for help!!!! – JacekRobak85 Nov 04 '14 at 13:26