Asp.net Mvc action返回多个模型实体给view

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" })
原文地址:https://www.cnblogs.com/qk2014/p/7260173.html