Results 1 to 3 of 3

Thread: MVC 3: Passing IEnumerable Model to Controller using HttpPost

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    MVC 3: Passing IEnumerable Model to Controller using HttpPost

    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

    Code:
    public class UserRoleModel
    	{
    		public string Username { get; set; }
    
    		[Display(Name = "User is in Role")]
    		public bool UsersInrole { get; set; }
    	}
    Code:
    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
    Code:
    @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
    Code:
    	[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
    Last edited by erickwidya; Jun 17th, 2011 at 11:39 PM.

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width