使用实体数据在mvc中从Viewmodel创建详细信息模板视图

时间:2016-11-19 14:49:07

标签: c# entity-framework asp.net-mvc-4

我正在尝试创建一个详细信息视图,其中我可以从实体DataModel显示所有数据: 这是ViewModel:

     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 个答案:

答案 0 :(得分:1)

它显示的方式是因为您传递到视图中的模型没有属性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的更多信息。