如何创建一个视图与多个模型MVC 4?(how to create a view with mult

2019-09-01 03:23发布

所以我的作品在asp.net MVC 4项目,我在我看来,有一个问题,我想是创建2不同的充型模型的观点,第一种观点(指数)采取的IEnumerable(Models.myModel)第二(用户详细信息)采取Models.myModel, 这是我的型号代码:

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; }
  }

我的控制器代码:

 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);
    }
 }

我的索引视图:

  @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>

我详细查看:

@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>

所以请,如果有人有任何想法或解决方案,我将非常感激:NB:此图片描述我想要什么

Answer 1:

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

public class CompoundViewModel
{
    public IEnumerable<SubscribersModel> AllSubscribers {get; set;} 
    public SubscribersModel SelectedSubscriber {get; set;}
}

理想的情况下也拆分视图成局部视图并使用呈现您的化合物模型的这两个部分放进去DisplayFor<>EditorFor<> 这样,你可以重用视图的“SubscriberModel”在申请的其他地方,如果你需要它。

控制器代码也可以使用依赖注入框架(如Autofac)注入那些你目前newing了依赖性提高。

另一种选择,因为你使用的是同一型号的列表和详细信息视图,将使用Javascript来处理这个客户端,无论是使用jQuery或手动允许客户端模型类似淘汰赛结合的新的框架之一.js文件或Angular.js。



Answer 2:

你只需要创建了第一个视图Details这是强烈类型SubscribersModel

@model _3SDWebProject.Models.SubscribersModel

在你的代码的任何地方右键单击Details()动作
选择添加查看...
离开“细节”的视图名
选中“创建一个强类型的视图”
选择您SubscribersModel从Model类下拉
脚手架模板:详细
点击“添加”



文章来源: how to create a view with multiple models mvc 4?