gpt4 book ai didi

ASP.NET MVC。不知道如何使用 Url.action 将对象传递给 Controller

转载 作者:行者123 更新时间:2023-12-01 01:37:49 25 4
gpt4 key购买 nike

我是 ASP.NET MVC 的新手。我能够创建我的 View 并显示数据(Gridview)。此外,我能够创建一个传递字符串和整数类型的超链接(使用 Url.Action)。但是,我想创建一个引用更复杂类型的超链接。与我的 View 关联的类引用了一个列表。我想要的是在我的 Controller 中创建一个额外的 ActionResult 作为参数列表(见下文)

public ActionResult ViewItems(List<Items> c)
{
return View(c);
}

我的想法是什么时候能够将该列表传递给 Controller ​​,然后 Controller 将调用相应的 View 。我试过(见下文)但我只是空白。
<asp:HyperLink ID="LinkContractID" runat="server" NavigateUrl='<%#Url.Action("ViewItems", new {c = **((Contract)Container.DataItem).ContractItems.ToList<Items>(**)}) %>'
Text='<%# Eval("ContractId") %>'></asp:HyperLink>

最佳答案

就像在上一个答案中一样,您不使用 asp 控件。但是,Html.ActionLink 有利有弊,例如,如果您想在图像周围放置链接,则它并不是那么好。在这种情况下,语法将是

<a href="<%= Url.Action(
"ShowListPage", "MyController", new { modelId = 101 }) %>">
<img src="img.gif" />
</a>

同样在 Controller 中使用您的操作,理想情况下,您希望这样做并让模型传递到强类型化到该模型的 View 。例如,您有一个带有构造函数的模型对象,该对象带有一个 id
public MyModel(int modelId)
{
this.TheListThatHoldsTheGridData = MyDataLayerProc(modelId);
}

这样你就可以在 MyController Controller 中执行你的操作,像这样返回 View ShowListPage(与 MyModel 实例关联)
public ActionResult ShowListPage(int modelId)
{
return View(new MyModel(modelId));
}

希望这可以帮助,

标记

关于ASP.NET MVC。不知道如何使用 Url.action 将对象传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/723044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com