2016-11-19 86 views
-1

我想创建一个详细信息视图在那里我可以有一个从实体DataModel中显示的所有数据的详细信息模板视图从视图模型: 这是视图模型:创建MVC中使用实体数据

 public class SubjectOverviewViewModel 
     { 
      [DisplayName("Neptunkód")] 
      public Subject NeptunId{ get; set; } 
      public SubjectContent TaFoAdatok { get; set; } 
      public SubjectContent TaAdatok { get; set; } 
      public SubjectContent TaOktatok { get; set; } 
      public SubjectContent TaKurzusok { get; set; } 
      public SubjectContent IrodalomLista { get; set; } 
      public virtual SubjectContent SubjectContent { get; set; } 

      public virtual Subject Subject { get; set; } 
     } 

这是控制器:

public ActionResult Details(SubjectOverviewViewModel model,string id) 
      { 

       return View(model); 
      } 

和视图:

@model irfwebpage.ViewModels.SubjectOverviewViewModel 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Details</h2> 

<div> 
    <h4>SubjectOverviewViewModel</h4> 
    <hr /> 
    <dl class="dl-horizontal"> 
     <dt> 
      @Html.DisplayNameFor(model => model.Subject.NeptunId) 
     </dt> 
     <dd> 
      @Html.DisplayFor(model => model.Subject.NeptunId) 
     </dd> 
     <dt> 
      @Html.DisplayNameFor(model => model.SubjectContent.TaFoAdatok) 
     </dt> 
     <dd> 
      @Html.DisplayFor(model => model.SubjectContent.TaFoAdatok) 
     </dd> 
     <dt> 
      @Html.DisplayNameFor(model => model.SubjectContent.TaAdatok) 
     </dt> 
     <dd> 
      @Html.DisplayFor(model => model.SubjectContent.TaAdatok) 
     </dd> 
     <dt> 
      @Html.DisplayNameFor(model => model.SubjectContent.TaOktatok) 
     </dt> 
     <dd> 
      @Html.DisplayFor(model => model.SubjectContent.TaOktatok) 
     </dd> 
     <dt> 
      @Html.DisplayNameFor(model => model.SubjectContent.TaKurzusok) 
     </dt> 
     <dd> 
      @Html.DisplayFor(model => model.SubjectContent.TaKurzusok) 
     </dd> 
     <dt> 
      @Html.DisplayNameFor(model => model.SubjectContent.IrodalomLista) 
     </dt> 
     <dd> 
      @Html.DisplayFor(model => model.SubjectContent.IrodalomLista) 
     </dd> 
     <dt> 
      @Html.DisplayNameFor(model => model.Subject.Name) 
     </dt> 
     <dd> 
      @Html.DisplayFor(model => model.Subject.Name) 
     </dd> 
    </dl> 
</div> 
<p> 
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) | 
    @Html.ActionLink("Back to List", "Index") 
</p> 

And this is on the website 该网站看起来像这样后,我启动它。我不知道什么似乎是问题。请帮忙。谢谢

回答

1

据显示,因为要传递到视图的模型为propertie SubjectSubjectContent没有价值的方式。

为确保您的视图正确显示模型,您可以在代码中创建一个虚拟SubjectOverviewViewModel,然后将其传递给控制器​​中的视图。一旦你得到这个工作,那么你知道一切都很好。之后,您可以从数据源获取数据,如果出现问题,您知道它不在显示器中,问题在于从数据源获取数据的代码。我不确定你是否熟悉单元测试,但在这里会非常有用。如果你不是,只需对它进行硬编码,然后一旦你的显示器工作,你可以注释掉它,并从数据源获取它。

话虽如此,我不确定你在做什么。我想你想要的是2个动作:

public ActionResult Details(string id) 
{ 
    // This will be GET action. 
    // Notice this only has one parameter: string id 
    // The browser will pass in an id, and your controller will get the 
    // info related to that id to display it. 
    // Write code here to get the data for your viewmodel from a datasource. 
    // Normally this will be a database or a web service. There are many 
    // different ways you can achieve this: some people use a repository 
    // pattern. 
    // To get the display working, you can just create a dummy one here 
    // and once you know it is displaying, you can then comment it out or 
    // totally remove it. 
    return View(model); 
} 

然后,你需要第二个动作:

public ActionResult Details(SubjectOverviewViewModel model) 
{ 
    // This will be a POST action 
    // Notice the parameter here is SubjectOverviewModel. 
    // Normally this will be passed in after the user has filled our some 
    // form. You will perform validation here and if it is valid, then you 
    // will save it to the datasource (database etc.). 
    // Once you save to the database send a redirect (RedirectToAction) to 
    // tell the user everything was successful. Use the PRG pattern here. 
    return View(model); 
} 

你可以阅读更多关于PRG模式here

相关问题