Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How Can I Send List<int> Roles Model? For example from view To Controller.
Using Ajax Jquery in Mvc3 asp.net not razor.
I'm using this code

var url = '<%:Url.Action("Roles","RolesManager") %>';
$.ajax({
    url: url,
    type: 'post',
    dataType: "json",
    traditional: true,
    data: $('#EditUserForm').serialize(),
    success: function () {
        $('#EditUserForm').submit();
    },
    error: function () {
    }
});

but when I debug the controller List<int> Roles = null.

mode in page like

<%: Html.ListBoxFor(m => m.UserRoles, new MultiSelectList(Model.UserRoles, "UserRoleId", "UserRoleName"), new { @id = "UserRoles", @class = "ddlUserRolesCls" })%>
share|improve this question
    
Are you using an editor template ? How your views looks ? –  Shyju Jun 3 '13 at 15:13
    
did u set ur parameter correctly? –  Atish Dipongkor Jun 3 '13 at 15:16

2 Answers 2

It's a Model Bind List, in this case, you have to send the information using the same name of your parameter in the name attribute of html inputs tag and asp.net model binder will convert it on a collection on the post. Your javascript looks fine. Try something like this:

In the view:

<input type="text" name="roles" value="1" />
<input type="text" name="roles" value="4" />
<input type="text" name="roles" value="2" />
<input type="text" name="roles" value="8" />

in the controller:

public ActionResult Post(List<int> roles) 
{
    // process
}

Also take a look at this article: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

share|improve this answer

Change your viewmodel like this. Note that we have a SelectedUserRoles property of type int array.

public class EditUserRole
{  
  public List<SelectListItem> UserRoles{ get; set; }
  public int[] SelectedUserRoles { set; get; }
  //Also other relevant properties here
}

And in my Get Action, fill the UserRoles property

public ActionResult EditUser(int id)
{
  var vm=new EditUserRole();
  vm.UserRoles=GetUserRoles();
}
public List<SelectListItem> GetUserRoles()
{
  var roles= new List<SelectListItem>();
  // the below is hardcoded. Get it from DB And fill it here
  roles.Add(new SelectListItem { Value="1",Text="Admin" });
  roles.Add(new SelectListItem { Value = "2", Text = "Editor" });
  return roles;
}

and in your view which is strongly typed to EditUserRole,

@Html.ListBoxFor(m => m.SelectedUserRoles,
              new MultiSelectList(Model.UserRoles, "Value", "Text"),
                          new { @id = "UserRoles", @class = "ddlUserRolesCls" })

When you post your form, you will get the selected Roles ID in the SelectedUserRoles property of the posted model.

[HttpPost]
public ActionResult Edit(EditUserRole model)
{
  // check model.SelectedUserRoles
  // to do : Save and redirect
}
share|improve this answer
    
i needed all item in list not selected –  M.Nabil Jun 3 '13 at 15:43
    
i select roles from list and add selected roles to another –  M.Nabil Jun 3 '13 at 15:46
    
why do you need all items ? You can get it in your POST action in the same way you got in GET action. –  Shyju Jun 3 '13 at 15:47
    
because i have two Html ListBoxFor the first for roles and another for selected roles i needed to get all item from the second Html.ListBoxFor appove code get me the last selected item –  M.Nabil Jun 3 '13 at 15:50
    
use jquery for each loop to get the items from the second item and the send it. –  Shyju Jun 3 '13 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.