PDA

Click to See Complete Forum and Search --> : VS 2010 MVC 3: Passing IEnumerable Model to Controller using HttpPost


erickwidya
Jun 17th, 2011, 09:46 PM
i can pass IEnumerable Model to View in Controller but when user edit in, how to get those value back using HttpPost because it contains IEnumerable??

currently i'm using this to passing the Model

public class UserRoleModel
{
public string Username { get; set; }

[Display(Name = "User is in Role")]
public bool UsersInrole { get; set; }
}
public ActionResult UsersRole(string roleName)
{
string[] usersInRole = Roles.GetUsersInRole(roleName);
MembershipUserCollection user = Membership.GetAllUsers();

List<UserRoleModel> list = new List<UserRoleModel>();

foreach (MembershipUser item in user)
{
UserRoleModel model = new UserRoleModel();
model.Username = item.UserName;

if (usersInRole.Contains(item.UserName))
{
model.UsersInrole = true;
}
else
{
model.UsersInrole = false;
}

list.Add(model);
}

return View(list);
}
my cshtml looks like this
@model IEnumerable<TopTech.Models.UserRoleModel>
@{
ViewBag.Title = "UsersRole";
}
<h2>
UsersRole</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{

<table>
<tr>
<th>
Username
</th>
<th>
User in Role
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Username)
</td>
<td>
@Html.EditorFor(modelItem => item.UsersInrole)
</td>
</tr>
}
</table>
<input type="submit" value="Submit" />
}

when it press Submit, the Model that passing contains null or empty..why? how to passed it??
the code looks like this
[HttpPost]
public ActionResult UsersRole(List<UserRoleModel> model)
{
return View();
}

basically i want user to be able to check or uncheck Checkbox then make the value available when POST it

thx,
erick

Krokonoster
Jun 18th, 2011, 04:22 AM
See if this help giving you some insight :
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

erickwidya
Jun 19th, 2011, 10:49 PM
thx Krokonoster for the link

i realize we can use something like that but not sure in mvc 3 :(..

it seems my controller doesn't accept the IEnumerable parameter that i pass, only display null when i debug it :(

very very confuse now