ASP.NET MVC. I don’t know how to use Url.action to pass an object to the controller

I am new to asp.net MVC. I managed to create my view and display the data (Gridview). In addition, I managed to create a hyperlink (using Url.Action) that passes strings and int types. However, I want to create a hyperlink that refers to a more complex type. The class associated with my view has a link to a list. I want to create an additional ActionResult in my controller that receives as a List parameter (see below)

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

My idea is when you need to pass this list to the controller, and then the controller will call the corresponding view. I tried (see below), but I'm just empty.

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

As in the previous answer, you are not using asp controls. However, there are also disadvantages with Html.ActionLink, this is not so good if you want, for example, to put a link to the image. In this case, the syntax will be

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

Also, with your action in the controller, you would ideally have to do this and get a model in order to get to a view that is strongly typed for that model. Thus, you have a model object with a constructor with an identifier, for example

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

So you can have your actions in the MyController controller, return a ShowListPage view (associated with the MyModel instance), for example,

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

Hope this helps,

Mark

+3
source

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

+1
source

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

0
source

All Articles