在asp.net中显示多个视图

时间:2016-04-04 07:35:20

标签: c# asp.net-mvc

我使用动态模型将多个模型传递给我的视图,但现在我不知道如何显示模型。

如何使用动态模型执行此操作?

如果您缺少信息,请随时询问。

2 个答案:

答案 0 :(得分:0)

 public ActionResult Index()
    {
        ViewBag.Message = "Welcome to my demo!";
        dynamic mymodel = new ExpandoObject();
        mymodel.Teachers = GetTeachers();
        mymodel.Students = GetStudents();
        return View(mymodel);
    }

在你的视图中

@using YourProjNameSpace;
@model dynamic
@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>

<p><b>Teacher List</b></p>

<table>
    <tr>
        <th>Id</th>
        <th>Code</th>
        <th>Name</th>
    </tr>
    @foreach (Teacher teacher in Model.Teachers)
    {
        <tr>
            <td>@teacher.TeacherId</td>
            <td>@teacher.Code</td>
            <td>@teacher.Name</td>
        </tr>
    }
</table>

<p><b>Student List</b></p>

<table>
    <tr>
        <th>Id</th>
        <th>Code</th>
        <th>Name</th>
        <th>Enrollment No</th>
    </tr>
    @foreach (Student student in Model.Students)
    {
        <tr>
            <td>@student.StudentId</td>
            <td>@student.Code</td>
            <td>@student.Name</td>
            <td>@student.EnrollmentNo</td>
        </tr>
    }
</table>

答案 1 :(得分:0)

像这样创建你的模型

public class ExpandoObject
{
  public List<Teacher> ListTeachers {get; set;}
   public List<Student> ListStudent { get; set;}
}

然后你的行动

public ActionResult DisplayModel()
        {
            ExpandoObject yourModel = new ExpandoObject();
            List<Teacher> listofteachers = dbh.GetTeachers();
            List<student> listofstudents = dbh.Students();

            mymodels.ListTeachers = listofteachers;
            mymodels.ListStudents = listofstudents;

            return View("ModelView", yourModel );
        }

,你的观点应该是这样的

    @model ExpandoObject
    <head>
        <link href="~/Content/test.css" rel="stylesheet" />
    </head>
    <body>
        @using (Html.BeginForm("DisplayModel", "Home"))
        {
            <table>
                @for (int i = 0; i < Model.ListTeachers.Count(); i++)
                {
                    <tr class="donatenow">
                        @Html.HiddenFor(m => m[i].ID)
                        @Html.HiddenFor(m => m[i].Name)
                        @Html.HiddenFor(m => m[i].Age)
                        <td>
                            <input type="radio" id="@i" name="amount" onclick="javascript: this.form.submit();" />
                            <label for="@i">@Html.DisplayFor(m => m.ListTeachers[i].Name)</label>
                        </td>
                    </tr>
                }

            </table>

 <table>
                @for (int i = 0; i < Model.ListStudents.Count(); i++)
                {
                    <tr class="donatenow">
                        @Html.HiddenFor(m => m[i].ID)
                        @Html.HiddenFor(m => m[i].Name)
                        @Html.HiddenFor(m => m[i].Age)
                        <td>
                            <input type="radio" id="@i" name="amount" onclick="javascript: this.form.submit();" />
                            <label for="@i">@Html.DisplayFor(m => m.ListStudents[i].Name)</label>
                        </td>
                    </tr>
                }

            </table>
        }
    </body>

展示模型的最佳方法是为每个集合使用部分视图。