zoukankan      html  css  js  c++  java
  • 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" })
  • 相关阅读:
    JSP九大内置对象的作用和用法总结(转)
    Java web的几种异常处理 (转)
    response.getWriter().write()与out.print()的区别(转)
    【JavaWeb】Session(转)
    java web中cookies的用法 转
    1123
    1120
    jsp 内置对象
    include与jsp:include区别
    11.24作业1
  • 原文地址:https://www.cnblogs.com/qk2014/p/7260173.html
Copyright © 2011-2022 走看看