如何使用mvc 4多个模型创建视图?

Moh*_*dov 2 asp.net-mvc entity-framework razor asp.net-mvc-4

所以我在asp.net mvc 4项目工作,我在我的视图中有一个问题,我想要的是创建一个具有2种不同类型的模型的视图,第一个视图(索引)采用IEnumerable(Models.myModel)第二个(订阅者详细信息) )拿Models.myModel,这是我的Model代码:

public class SubscribersModel
{

    [Required]
    [DataType(DataType.Text)]
    public string name { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [StringLength(maximumLength: 10, MinimumLength = 7)]
    public string cin { get; set; }

    [Required]
    [DataType(DataType.Date)]
    public DateTime birthday { get; set; }

    [DataType(DataType.Text)]
    public string birthplace { get; set; }
  }
Run Code Online (Sandbox Code Playgroud)

我的控制器代码:

 public class SubscribersController : Controller
{
    private AgencyDbEntities dbcontext = new AgencyDbEntities();
    private Subscribe sb = new Subscribe();

    public ActionResult Index()
    {
        var subscribers = from sb in dbcontext.Subscribes select new SubscribersModel {      cin = sb.cin, name = sb.name, birthday = (DateTime)sb.birthDay, birthplace = sb.birthPlace   };

        return View(subscribers);
    }

    public ActionResult Details(string id,string cin,string name)
    {
        var subscriber = new SubscribersModel();
        IEnumerable<Subscribe> list = from s in dbcontext.Subscribes select s;
        foreach (var sb in list)
        {
            if (sb.cin == id)
            {
                subscriber.cin = sb.cin;
                subscriber.name = sb.name;
                subscriber.birthday = (DateTime)sb.birthDay;
                subscriber.birthplace = sb.birthPlace;
            }
        }
        return View(subscriber);
    }
 }
Run Code Online (Sandbox Code Playgroud)

我的索引视图:

  @model IEnumerable<_3SDWebProject.Models.SubscribersModel>

@{
    ViewBag.Title = "Subscribers";
}
    <div class="sidebar">
      //here i want to show my details view
     </div>
 <div class="content" style="width: 700px; margin-left: 250px; height: 545px;margin-  top: -30px;">
  <h2>Subscribers</h2>
<p>
     @Html.ActionLink("Create New", "Create")
</p>

 @Styles.Render("~/Content/css")
 <script src="@Url.Content("~/Scripts/Table.js")" type="text/javascript"></script>
 <table class="altrowstable" id="alternatecolor">
 <tr>
    <th>
        CIN
    </th>
    <th>
        Name
    </th>
    <th>
        birthday
    </th>
    <th>
        birthplace
    </th>
     <th class="operations">
        Operations
    </th>
</tr>

 @foreach (var item in Model)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.cin)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.birthday)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.birthplace)
    </td>
    <td>
      @Html.ActionLink("Details", "Details", new { id = item.cin }, new { @class = "details-logo"})
     </td>
</tr>
}

</table>
</div>
Run Code Online (Sandbox Code Playgroud)

我的详情视图:

@model _3SDWebProject.Models.SubscribersModel

<fieldset>
<legend>SubscribersModel</legend>

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

<div class="display-label">
     @Html.DisplayNameFor(model => model.birthplace)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.birthplace)
</div>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

所以,如果有人有任何想法或解决方案,我将非常感激:NB:这张照片描述了我想要的 在此输入图像描述

Ian*_*cer 9

创建包含这两种视图模型的新的复合视图模型.

public class CompoundViewModel
{
    public IEnumerable<SubscribersModel> AllSubscribers {get; set;} 
    public SubscribersModel SelectedSubscriber {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,您还可以将视图拆分为部分视图,并使用DisplayFor<>或将复合模型的这两部分渲染到它们中EditorFor<>.这样,如果需要,您可以在应用程序的其他位置重用"SubscriberModel"视图.

通过使用依赖注入框架(例如Autofac)来注入您当前正在创建的依赖项,也可以改进您的控制器代码.

另一个替代方案,假设您使用相同的模型列表和详细信息视图,将使用Javascript手动处理此客户端,手动使用jQuery或允许客户端模型绑定的一个较新的框架,如Knockout .js或Angular.js.