[英]How can send List<> Model From View To Controller using Ajax Jquery in Mvc3 asp.net?
How Can I Send List<int>
Roles Model? 如何发送List<int>
角色模型? For example from view To Controller. 例如,从视图到控制器。
Using Ajax Jquery in Mvc3 asp.net not razor. 在Mvc3 asp.net中使用Ajax Jquery不剃须刀。
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
. 但是当我调试控制器List<int> Roles = null
。
mode in page like 页面模式
<%: Html.ListBoxFor(m => m.UserRoles, new MultiSelectList(Model.UserRoles, "UserRoleId", "UserRoleName"), new { @id = "UserRoles", @class = "ddlUserRolesCls" })%>
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. 这是一个模型绑定列表,在这种情况下,您必须在html input标签的name
属性中使用与参数相同的名称来发送信息,而asp.net模型联编程序会将其转换为帖子的集合。 Your javascript looks fine. 您的JavaScript看起来不错。 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 还可以看看这篇文章: http : //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Change your viewmodel like this. 像这样更改您的视图模型。 Note that we have a SelectedUserRoles
property of type int array . 注意,我们有一个int array类型的SelectedUserRoles
属性。
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 然后在我的“获取操作”中,填充UserRoles
属性
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
, 在您的视图中,该视图的类型为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. 发布表单时,您将在发布模型的SelectedUserRoles
属性中获得选定的角色ID。
[HttpPost]
public ActionResult Edit(EditUserRole model)
{
// check model.SelectedUserRoles
// to do : Save and redirect
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.