ASP.net MVC uses multiple models or datasets for the same view or page

Source: Internet
Author: User
Tags actionlink

I am not good at writing blogs... the content should be as simple as possible... just for example ..

In asp.net MVC, only one Model can be returned by default at a time. If multiple datasets are called, ViewData can only be used for export. This seems troublesome, you can implement the model extension method... I am only an entry-level learner. I may not understand or describe it clearly. If there is a better method or suggestion.

First, define a class in the controller. The content is as follows:

Public class lviewModel
{
Public List <Sorts> sortList {get; private set ;}
Public List <Articles> artList {get; private set ;}
Public lviewModel (List <Models. Sorts> sortlists, List <Models. Articles> artlists)
{
This. sortList = sortlists;
This. artList = artlists;
}

}

To receive two lists and then return

Models. ESCMSContext escms = new ESCMS_MVC_ SQL .Models.ESCMSContext ();

Return View (new lviewModel (escms. sortList (), escms. artList ()));

When using

The first line of the View File to be referenced

<% @ Page Title = "" Language = "C #" MasterPageFile = "~ /Views/Shared/Site. Master "Inherits =" System. Web. Mvc. ViewPage <ESCMS_MVC_ SQL .Controllers.lviewModel> "%>

Change ESCMS_MVC_ SQL to the name of your project.

Then you can

Model. artList

Model. sortList

This is used.

<Asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">

<Form id = "form1" runat = "server">

<H2> list
<Table>
<Tr>
<Th> </th>
<Th>
ArtID
</Th>
<Th>
ArtTitle
</Th>
<Th>
ArtSort
</Th>
<Th>
ArtSort
</Th>
</Tr>

<% Foreach (var item in Model. artList)
{%>

<Tr>
<Td>
<% = Html. ActionLink ("Edit", "Edit", new {id = item. ArtID}) %> |
<% = Html. ActionLink ("Details", "Details", new {id = item. ArtID}) %>
</Td>
<Td>
<% = Html. Encode (item. ArtID) %>
</Td>
<Td>
<% = Html. Encode (item. ArtTitle) %>
</Td>
<Td>
<% = Html. Encode (item. ArtSort) %>
</Td>
<Td>
<% = Html. Encode (item. Sorts. SortName) %>
</Td>
</Tr>

<% }%>

</Table>

<Table>
<Tr>
<Th> aa </th>
<Th> bb </th>
</Tr>

<% Foreach (var item in Model. sortList)
{%>

<Tr>
<Td>
<% = Html. ActionLink ("Edit", "Edit", new {/** // * id = item. PrimaryKey */}) %> |
<% = Html. ActionLink ("Details", "Details", new {/** // * id = item. PrimaryKey */}) %>
</Td>
<Td>
<% = Html. Encode (item. SortName) %>
</Td>
</Tr>

<% }%>

</Table>
</Form>

</Asp: Content>

In this way, it's quite simple. It's just a bit complicated. I think Microsoft should provide such a class by default, which is easy to write ..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.