简体   繁体   English

如何使用Mvc3 asp.net中的Ajax Jquery从视图向控制器发送List <>模型?

[英]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.

相关问题 如何在ASP.NET MVC中使用jQuery ajax方法将数据列表发送到Controller Action方法? - How to send a list of data to Controller Action method using jQuery ajax method in ASP.NET MVC? 我如何不将在asp.net Mvc3中使用Ajax / JQuery的checkBox值传递给asp.net mvc 3中的控制器操作 - How can i Pass values of checkBox those are checked to controller action in asp.net mvc 3 without using Ajax/JQuery in asp.net Mvc3 如何在 ASP.NET Razor 视图中使用 jQuery 将模型发送回 ASP.NET 控制器 - How send the model back to an ASP.NET controller using jQuery in a ASP.NET Razor view 发送清单<AnonymousType>从控制器到 ASP.Net MVC 中的视图 - Send list<AnonymousType> from controller to view in ASP.Net MVC 如何在ASP.NET MVC 3中从控制器发送值以进行查看? - How to send value from controller to view in asp.net mvc 3? 如何在MVC ASP.NET中使用AJAX将模型从View发送到bool方法 - How to send model from View to bool method with AJAX in MVC ASP.NET 如何从 AJAX 方法返回列表以从 controller ASP.NET MVC 查看 - How to return a list from AJAX method to view from controller ASP.NET MVC 带列表的ASP.NET MVC3模型 - ASP.NET MVC3 model with list 如何通过Jquery.ajax()将字符串值从视图传递到ASP.NET MVC控制器 - How to pass string value from a view via Jquery.ajax() to ASP.NET MVC controller ASP.NET MVC3无法解析jQuery $ .ajax()发送的列表类型的URL参数 - ASP.NET MVC3 can't parse URL parameter of a list type sent by jQuery $.ajax()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM