Asp.net Mvc action返回多個模型實體給view

來源:互聯網
上載者:User

標籤:info   多個   type   hold   device   style   返回   tail   slave   

1、controller中action代碼:

public class HomeController : Controller    {        public ActionResult Detail(int id)        {            UserInfo master = masterBLL.QueryOne(x => x.StudentID == id);//主表            UserSlave slave = slaveBLL.QueryOne(x => x.StudentID == id);//從表            return View(Tuple.Create(master, slave));        }    }

Tuple是c#4.0的新特性。

如果返回三個,則 Tuple.Create(master, slave1 , slave2)

 

2、view代碼:

@{    Layout = null;}@model Tuple<Model.UserInfo, Model.UserSlave><!DOCTYPE html><html><head>    <title></title>    <meta name=‘viewport‘ content=‘width=device-width,initial-scale=1.0‘></head><body>    <table>        <tr>            <td><span>姓名</span></td>            <td>                @Html.DisplayFor(model => model.Item1.Name)            </td>        </tr>        <tr>            <td><span>郵箱</span></td>            <td>                @Html.DisplayFor(model => model.Item2.Email)            </td>        </tr>    </table></body></html>

model.Item1表示實體模型UserInfo,model.Item2表示實體模型UserSlave

如果是textbox控制項,寫法一樣:

@Html.TextBoxFor(model => model.Item1.Name, new { placeholder = "姓名", maxlength = "20" })

 

Asp.net Mvc action返回多個模型實體給view

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.