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

时间:2022-11-30 21:13:43

I am new to asp.net MVC. I was able to create my view and display the data (Gridview). Additionally, I was able to create a hyperlink (using Url.Action) passing string and int types. However, I want to create a hyperlink that it is referencing a more complex type. The class associated with my view has a reference to a List. What I want is to create an additional ActionResult in my controller that gets as a parameter List (See below)

我是asp.net MVC的新手。我能够创建我的视图并显示数据(Gridview)。此外,我能够创建一个超链接(使用Url.Action)传递字符串和int类型。但是,我想创建一个引用更复杂类型的超链接。与我的视图关联的类具有对List的引用。我想要的是在我的控制器中创建一个额外的ActionResult作为参数列表(见下文)

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

My idea is when is to be able to pass that List to the controller and then the controller will call the corresponding view. I tried (See below) but I just get blank.

我的想法是什么时候能够将该List传递给控制器​​,然后控制器将调用相应的视图。我试过(见下文),但我只是空白。

<asp:HyperLink ID="LinkContractID" runat="server" NavigateUrl='<%#Url.Action("ViewItems", new {c = **((Contract)Container.DataItem).ContractItems.ToList<Items>(**)}) %>'
Text='<%# Eval("ContractId") %>'></asp:HyperLink>

3 个解决方案

#1


Like in the previous answer, you don't use asp controls. There are pros and cons with Html.ActionLink however, it isn't so good if you want to put a link around an image for instance. In this case the syntax would be

与前面的答案一样,您不使用asp控件。 Html.ActionLink有利有弊,但是如果你想在图像周围放置链接则不太好。在这种情况下,语法将是

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

Also with your action in the controller, you would ideally be looking to have this go and get the model to pass to a view strongly typed to this model. So you have a model object with a constructor taking an id, for instance

另外,在控制器中执行操作时,理想情况下,您可以将此模型传递给强烈键入此模型的视图。例如,你有一个带有构造函数的模型对象

public MyModel(int modelId)
{
   this.TheListThatHoldsTheGridData = MyDataLayerProc(modelId);
}

This way you can have your action in the MyController controller, return the view ShowListPage (associated with a MyModel instance) like so

这样你可以在MyController控制器中执行操作,返回视图ShowListPage(与MyModel实例关联),就像这样

public ActionResult ShowListPage(int modelId)
{
   return View(new MyModel(modelId));
}

Hope this helps,

希望这可以帮助,

Mark

#2


If you are looking for a grid, this tutorial shows how to create a grid with MVC.

如果您正在寻找网格,本教程将介绍如何使用MVC创建网格。

#3


With MVC, you shouldn't use Gridview and asp: controls. If you want to generate a link, just use <%=Html.ActionLink(...) %> with the necessary parameters.

使用MVC,您不应该使用Gridview和asp:控件。如果要生成链接,只需使用<%= Html.ActionLink(...)%>和必要的参数。

#1


Like in the previous answer, you don't use asp controls. There are pros and cons with Html.ActionLink however, it isn't so good if you want to put a link around an image for instance. In this case the syntax would be

与前面的答案一样,您不使用asp控件。 Html.ActionLink有利有弊,但是如果你想在图像周围放置链接则不太好。在这种情况下,语法将是

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

Also with your action in the controller, you would ideally be looking to have this go and get the model to pass to a view strongly typed to this model. So you have a model object with a constructor taking an id, for instance

另外,在控制器中执行操作时,理想情况下,您可以将此模型传递给强烈键入此模型的视图。例如,你有一个带有构造函数的模型对象

public MyModel(int modelId)
{
   this.TheListThatHoldsTheGridData = MyDataLayerProc(modelId);
}

This way you can have your action in the MyController controller, return the view ShowListPage (associated with a MyModel instance) like so

这样你可以在MyController控制器中执行操作,返回视图ShowListPage(与MyModel实例关联),就像这样

public ActionResult ShowListPage(int modelId)
{
   return View(new MyModel(modelId));
}

Hope this helps,

希望这可以帮助,

Mark

#2


If you are looking for a grid, this tutorial shows how to create a grid with MVC.

如果您正在寻找网格,本教程将介绍如何使用MVC创建网格。

#3


With MVC, you shouldn't use Gridview and asp: controls. If you want to generate a link, just use <%=Html.ActionLink(...) %> with the necessary parameters.

使用MVC,您不应该使用Gridview和asp:控件。如果要生成链接,只需使用<%= Html.ActionLink(...)%>和必要的参数。