如何通过一个过滤的列表为行动参数ASP.NET MVC [英] How to pass a filtered list into Action parameters ASP.NET MVC
问题描述
您好我有两列的的WebGrid:
1,名称搜索
2,动作绑定到另一个控制器动作。在点击我会
重新定向到该动作,参数应该是列表℃的清单;试验>
项目。
块引用>输出:
请求到达控制器,但参数为空colelction,我失去了什么。查看:
@model IEnumerable的<试验>
< DIV ID =testGrid>
@ {
VAR电网=新的WebGrid(ajaxUpdateContainerId:testGrid,canSort:真正的);
grid.Bind(模型);
@ MvcHtmlString.Create(
@ grid.GetHtml(
列:grid.Columns
(
grid.Column(Html.DisplayNameFor(型号=> model.Name).ToHtmlString()
标题:Html.DisplayNameFor(型号=> model.Name).ToHtmlString()),
grid.Column(动作,标题:操作,格式为:@< A HREF =@ Url.Action(LoadTest,NewController
新
{
配料= Model.Select(T => t.Id == @ item.Id).ToList()
}
)级=编辑-BTN>< / A>)
)
)的ToString()
)
}
< / DIV>控制器code
列表来空的。
公众的ActionResult LoadTest(列表<试验> testItems)
{
//...test code。
}
解决方案您Url.Action创造比你的控制器期待什么不同的变量:
无论是从改变你的成分动作,testItems:
grid.Column(行动,标题:操作,格式为:@< A HREF =@ Url.Action(LoadTest,NewController
新
{
testItems = Model.Select(T => t.Id == @ item.Id).ToList()
}或更改控制器期望参数调用成分:
公众的ActionResult LoadTest(列表<试验>成分)
{
//...test code。
}另外,你更新你的Global.asax期待这种类型的路线?这个例子假设您将该名称
testItems
作为参数。</ P>routes.MapRoute(
测试路线,//路线名称
NewController / {行动} / {} testItems,// URL带参数
新{控制器=NewController,行动=LoadTest} //参数默认
);Hi I have a WebGrid with two columns:
1-Name
2-Action binded to another controller action. On click i will re-direct to that action, parameter should be a List ofList<Test>
items.Output: Request goes to Controller but parameter is empty colelction, am i missing anything.
View:
@model IEnumerable<Test> <div id="testGrid"> @{ var grid = new WebGrid(ajaxUpdateContainerId: "testGrid", canSort: true); grid.Bind(Model); @MvcHtmlString.Create( @grid.GetHtml( columns: grid.Columns ( grid.Column(Html.DisplayNameFor(model => model.Name).ToHtmlString(), header: Html.DisplayNameFor(model => model.Name).ToHtmlString()), grid.Column("Action", header: "Action", format: @<a href="@Url.Action("LoadTest", "NewController", new { ingredients = Model.Select(t=>t.Id==@item.Id).ToList() } )" class="edit-btn"></a>) ) ).ToString() ) } </div>
Controller code
List coming empty.
public ActionResult LoadTest(List<Test> testItems) { //...test code. }
解决方案Your Url.Action is creating a different variable than what your controller is expecting:
Either change your action from ingredients, to testItems:
grid.Column("Action", header: "Action", format: @<a href="@Url.Action("LoadTest", "NewController", new { testItems = Model.Select(t=>t.Id==@item.Id).ToList() }
or change your controller to expect a parameter called ingredients:
public ActionResult LoadTest(List<Test> ingredients ) { //...test code. }
Also, did you update your Global.asax to expect a route of this type? This example is assuming you keep the name
testItems
as your parameter.routes.MapRoute( "Test route", // Route name "NewController/{action}/{testItems}", // URL with parameters new { controller = "NewController", action = "LoadTest" } // Parameter defaults );
这篇关于如何通过一个过滤的列表为行动参数ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!