Asp.net mvc 填充来自多个模型的值列表

Asp.net mvc 填充来自多个模型的值列表,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,如何创建由多个模型/屏幕引用的值列表 我已经创建了一个名为ListValues的模型,它将包含两个字段(ID,Name),我希望我的所有模型都能调用它,这是最好的方法。我也不想放松绑定。让您的所有模型都从ListValues继承。您可能希望将ListValues(现在是一个基类)重命名为更具描述性的类型,如ModelBase 下面是一个例子: ModelBase.cs namespace ListValuesTest.Models { public class ModelBase

如何创建由多个模型/屏幕引用的值列表


我已经创建了一个名为ListValues的模型,它将包含两个字段(ID,Name),我希望我的所有模型都能调用它,这是最好的方法。我也不想放松绑定。

让您的所有模型都从ListValues继承。您可能希望将ListValues(现在是一个基类)重命名为更具描述性的类型,如ModelBase

下面是一个例子:

ModelBase.cs

namespace ListValuesTest.Models
{
    public class ModelBase
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class Employee : ModelBase
    {
        public string Department { get; set; }
    }

    public class Customer : ModelBase
    {
        public string Application { get; set; }
    }
}
HomeController.cs

namespace ListValuesTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult EmployeeOfTheMonth()
        {
            ListValuesTest.Models.Employee NumberOneEmployee = new Models.Employee();
            NumberOneEmployee.ID = 1;
            NumberOneEmployee.Name = "Brian";
            NumberOneEmployee.Department = "IT";

            return View(NumberOneEmployee);
        }

        public ActionResult CustomerOfTheMonth()
        {
            ListValuesTest.Models.Customer NumberOneCustomer = new Models.Customer();
            NumberOneCustomer.ID = 1;
            NumberOneCustomer.Name = "Microsoft";
            NumberOneCustomer.Application = "Visual Studio";

            return View(NumberOneCustomer);
        }
    }
}
employeeofmonth.cshtml

@model ListValuesTest.Models.Employee

@{
    ViewBag.Title = "EmployeeOfTheMonth";
}

<h2>EmployeeOfTheMonth</h2>

<fieldset>
    <legend>Employee</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Department)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Department)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>
@model ListValuesTest.Models.Customer

@{
    ViewBag.Title = "CustomerOfTheMonth";
}

<h2>CustomerOfTheMonth</h2>

<fieldset>
    <legend>Customer</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Application)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Application)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>
@model ListValuesTest.Models.Employee
@{
ViewBag.Title=“每月的雇员”;
}
每月雇员
雇员
@DisplayNameFor(model=>model.Department)
@DisplayFor(model=>model.Department)
@DisplayNameFor(model=>model.Name)
@DisplayFor(model=>model.Name)

@ActionLink(“编辑”,“编辑”,新的{id=Model.id})|
@ActionLink(“返回列表”、“索引”)

CustomerOfTheMonth.cshtml

@model ListValuesTest.Models.Employee

@{
    ViewBag.Title = "EmployeeOfTheMonth";
}

<h2>EmployeeOfTheMonth</h2>

<fieldset>
    <legend>Employee</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Department)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Department)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>
@model ListValuesTest.Models.Customer

@{
    ViewBag.Title = "CustomerOfTheMonth";
}

<h2>CustomerOfTheMonth</h2>

<fieldset>
    <legend>Customer</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Application)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Application)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Name)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>
@model ListValuesTest.Models.Customer
@{
ViewBag.Title=“CustomerOfTheMonth”;
}
赫蒙斯的顾客
顾客
@DisplayNameFor(model=>model.Application)
@DisplayFor(model=>model.Application)
@DisplayNameFor(model=>model.Name)
@DisplayFor(model=>model.Name)

@ActionLink(“编辑”,“编辑”,新的{id=Model.id})|
@ActionLink(“返回列表”、“索引”)


我认为ViewModel将是您最好的解决方案,请在web上查找使用任何视图模型的解决方案……如果您遇到任何困难,请带着code来找我们。

您好,谢谢您的回答,请您提供一些关于如何编写相同视图模型的参考。我已经挣扎了两天了。提前谢谢。你的问题解决了吗?下面是一个链接或者我可以给你一些viewmodel的演示代码如果你想谢谢janina,如果你能分享一些viewmodal的代码,这将是非常有帮助的。我仍在努力。尝试过这样做,但出现错误,请提供一些代码参考。提前谢谢