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

1、controller中action代码:

public class HomeController : Controller
    {
        public ActionResu<  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 TupleModel.UserInfo, Model.UserSlave
!DOCTYPE html
html
head
    title/title
    meta name=viewport content=width=device-width,initial-scale=1.0
/head
body
    table
        tr
            tdspan姓名/span/td
            td
                @Html.DisplayFor(model = model.Item1.Name)
            /td
        /tr
        tr
            tdspan邮箱/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 = "姓名", maxlen >h = "20" })

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

原文地址:http://www.cnblogs.com/qk2014/p/7260173.html


最新回复(0)
/jishu8K8u2BfpaIR0kAkxqTpL24mdqCcF_2FmI2nHrJMw_3D_3D4719352
8 简首页