ASP.NET MVC - 获得查询字符串对象名单 [英] ASP.NET MVC - get list of objects from query string

查看:124
本文介绍了ASP.NET MVC - 获得查询字符串对象名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递的参数列表。如姓名,ID,类型。届时将有许多这样的URL,如下所示:

<$p$p><$c$c>\"Name=blah1,Id=231,Type=blah1;Name=blah2,Id=2221,Type=blah1;Name=blah3,Id=45411,Type=blah3;\"

我不知道是否有将这些查询参数映射为对象列表的方式。所以,我可以创建一个对象:

MyTestObject {名称,标识;类型},可以在我的控制器说

 指数(IList的&LT; MyTestObject&GT; PARAMS)

PARAMS将与来自查询字符串数据填充。

东西是类似的http:/ /haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx


解决方案

是ASP.NET MVC可以自动绑定集合行动参数,可以,但你必须通过你的PARAMS作为从值,而且它看起来像许多PARAMS你在查询字符串去传球。有看看这个<一个href=\"http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx\" rel=\"nofollow\">http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx

基本上你需要做什么:

1)创建CALSS这将包含您的PARAMS

 公共类MyParam
{
 publc INT标识{搞定;组;}
 公共字符串名称{;组;} //做所有的休息
}

2)创建模式,你会传递给你的看法

 公共类MyViewModel
{
  IList的&LT; MyParam&GT; MyParams {搞定;组;}
}

3)在 [HTTPGET] 操作创建收集和传递到您的视图:

  [HTTPGET]
公共虚拟的ActionResult指数()
{
   MyViewModel模式=新MyViewModel();
   model.MyParams = CreateMyParamsCollection();   返回查看(模型);
}

4)遍历您的收藏视图

  @model MyViewModel@ {INT指数= 0;}@foreach(在Model.MyParams MyParam细节)
{
  @ Html.TextBox(MyParams [+ index.ToString()+] .ID,detail.Id)
  @ Html.TextBox(MyParams [+ index.ToString()+] .Name点,detail.Name)  指数++;
}

5)比对你的 [HttpPost] 的行动可能会引起你PARAMS在集合

  [HttpPost]
公共虚拟的ActionResult指数(MyViewModel模型)
要么
[HttpPost]
公共虚拟的ActionResult指数(IList的&LT; MyParam&GT;模型)

P.S

此外,如果你想在控制器的所有表单PARAMS你可以简单的去这样的:

  [HttpPost]
公共虚拟的ActionResult指数(的FormCollection形式)

I'm passed a list of parameters. Such as "Name", "Id", "Type". There will be an many of these in url, like so:

"Name=blah1,Id=231,Type=blah1;Name=blah2,Id=2221,Type=blah1;Name=blah3,Id=45411,Type=blah3;"

I wonder if there is a way to map these query parameters to a List of objects. So, I can create an object:

MyTestObject {Name;Id;Type} and can say in my controller

Index(IList<MyTestObject> params)

params will be filled in with data from query string.

Something that is similar to http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

解决方案

Yes ASP.NET MVC could automatically bind collections to action params, but you need to pass your params as a from values, moreover it is looks like to many params you going pass in query string. Have look at this one http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx

Basically what you need to do:

1) Create your calss which would contain your params

public class MyParam 
{
 publc int Id {get; set;}
 public string Name {get; set;}

 //do all the rest
}

2) Create model which you would pass to your view

public class MyViewModel
{
  IList<MyParam> MyParams {get; set;}
}

3) Create your collection in your [HttpGet] action and pass that to your view:

[HttpGet]
public virtual ActionResult Index()
{
   MyViewModel model = new MyViewModel();
   model.MyParams = CreateMyParamsCollection();

   return View(model);
}

4) Iterate your collection in the view

@model MyViewModel

@{int index = 0;}

@foreach (MyParam detail in Model.MyParams)
{
  @Html.TextBox("MyParams[" + index.ToString() + "].Id", detail.Id)
  @Html.TextBox("MyParams[" + index.ToString() + "].Name", detail.Name)

  index++;
} 

5) Than on your [HttpPost] action you may catch your params in collection

[HttpPost]
public virtual ActionResult Index(MyViewModel model)
or
[HttpPost]
public virtual ActionResult Index(IList<MyParam> model)

P.S

Moreover if you want to get all your form params in controller you may simple go like that:

[HttpPost]    
public virtual ActionResult Index(FormCollection form)

这篇关于ASP.NET MVC - 获得查询字符串对象名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆