MVC和Entity Framework Html.DisplayNameFor与复合视图模型 [英] MVC and Entity Framework Html.DisplayNameFor with Composite ViewModel

查看:679
本文介绍了MVC和Entity Framework Html.DisplayNameFor与复合视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当舒适的使用MVVM WPF / Silverlight的,但是这是我以MVC Web应用程序第一次尝试...只是一个供参考的我的背景。

I’m fairly comfortable with MVVM using WPF/Silverlight but this is my first attempt at an MVC Web Application…just an fyi for my background.

我已经创建了一个名为TestSitesController控制器,它是从我的实体框架模型(即生成读/写操作和视图模板)网站模型类自动生成。我修改的唯一的事情是在3点存在的Guid ID的默认参数= NULL的一些方法。我刚刚摆脱了=空的一切工作正常。这里是什么,我改变了一个例子

I’ve created a controller called TestSitesController which was auto generated from from the "Site" model class in my Entity Framework Model (the template that generates the read/write actions and views). The only thing I modified was in 3 spots there was a default parameter of Guid id = null for some methods. I just got rid of the " = null" all works fine. Here is an example of what I changed

public ActionResult Delete(Guid id = null)
{
    //....
}

这已更改为

public ActionResult Delete(Guid id)
{
    //....
}

网站的模式是没有什么特别的SITEID,缩写,和显示名称...试图保持它尽可能简单了这个问题。好了,所以我运行的网站,并转到HTPP://.../TestSites/,一切完美的作品

The Site model is nothing special SiteId, Abbreviation, and DisplayName…trying to keep it as simple as possible for this question. Ok so I run the website and goto htpp://.../TestSites/ and everything works perfectly.

我发现我所有的意见(创建,删除,细节和编辑)使用的是@model MVCWeb.MyEntities.Site其中我与现在完全确定;但在Index.cshtml认为我注意到它是使用

I noticed that all of my views (Create, Delete, Details and Edit) are using an @model MVCWeb.MyEntities.Site which I’m perfectly ok with for now; but in the Index.cshtml view I noticed it was using an

@model IEnumerable<MVCWeb.MyEntities.Site>

这工作正常生成的模板,但我想用一个复合视图模式,也许这就是我想在我的MVVM的知识组合,但如果将所有可能坚持这一点。在我的脑海里一个复合视图模型只是一个模型是具体实现这一与像SelectedSiteId等附加属性一起组成的1个或多个实体模型视图。

This works fine for the generated template but I would like to use a "Composite View Model" and maybe this is where I’m trying to mix in my MVVM knowledge but would to stick with that if all possible. In my mind a Composite View Model is just a Model that is specific towards a view that is composed of 1 or more Entity Models along with additional properties like SelectedSiteId, etc.

所以,我创建了一个非常简单的视图模型称为TestSitesViewModel

So I created a very simple ViewModel called TestSitesViewModel

public class TestSitesViewModel
{
    //Eventually this will be added to a base ViewModel to get rid
    //of the ViewBag dependencies
    [Display(Name = "Web Page Title")]
    public string WebPageTitle;

    public IEnumerable<MVCWeb.MyEntities.Site> Sites;

    //Other Entities, IEnumberables, or properties go here
    //Not important for this example
}

然后在我的控制器,我添加了名为IndexWithViewModel的操作方法

Then in my controller I added an action method called IndexWithViewModel

public ActionResult IndexWithViewModel()
{
    var vm = new TestSitesViewModel();
    vm.WebPageTitle = "Sites With Composite View Model";
    vm.Sites = db.Sites.ToList();

    return View(vm);
}

我遂作出上述Index.cshtml的副本,并把它命名为IndexWithModel.cshtml以配合我的新的ActionResult方法名。我改的第一行

I then made a copy of the Index.cshtml and named it IndexWithModel.cshtml to match my new ActionResult method name. I changed the top line of

@model IEnumerable<MVCWeb.MyEntities.Site>

@model MVCWeb.Models.TestSitesViewModel

我添加了这个表款,用于测试的DisplayNameFor和DisplayFor

I added this before the table section to test for the DisplayNameFor and the DisplayFor

@Html.DisplayNameFor(model => model.WebPageTitle)
&nbsp;
@Html.DisplayFor(model => model.WebPageTitle)
<br />

改变了

@foreach (var item in Model) {

@foreach (var item in Model.Sites) {

和注释掉包含现在所有的表头的TR部分。一切都完美的作品除了@ Html.DisplayNameFor正在显示的WebPageTitle变量名,而不是使用网页标题,如

And commented out the tr section that contains all of the table headers for now. Everything works perfectly except that the @Html.DisplayNameFor is displaying the variable name of "WebPageTitle" instead of using "Web Page Title" as denoted in the Display attribute of the Data Annotation in

[Display(Name = "Web Page Title")]
public string WebPageTitle;

另外,如果我在包含表头信息,TR部分评论回来。我不能为我的生活弄清楚什么把我试过model.Sites.Abbreviation,model.Sites [0] .Abbreviation,以及其他各种组合,但得到的错误。

Also if I comment back in the tr section that contains the table header information. I can not for the life of me figure out what to put in I’ve tried model.Sites.Abbreviation, model.Sites[0].Abbreviation, and various other combinations but get errors.

<tr>
    <th>
        @Html.DisplayNameFor(model => model.Abbreviation)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DisplayName)
    </th>
    <th></th>
</tr>

所以,我应该怎么用呢?我不明白为什么model.Sites.Abbreviation不作为地方工作是作为原Index.cshtml模型类型完全相同

So what should I use there? I'm not quite sure why model.Sites.Abbreviation doesn't work as Sites is the exact same type that was used as the model in the original Index.cshtml

推荐答案

我终于瞎搞了几个小时后,想通了这一点。

I finally figured this out after messing around for several hours.

为了使用多媒体arrtibute在数据Anotations一个get和set acessor方法必须即使他们只是在默认的加1的问题。我假设这是因为他们必须仅在性能和一类不是公共的数据成员的工作。这里是code,以获得

1st problem in order to use the Display arrtibute in the Data Anotations a get and set acessor methods must be added even if they are only the default ones. I'm assuming this is because they must only work on properties and not public data members of a class. Here is code to get the

@Html.DisplayNameFor(model => model.WebPageTitle)

正常工作

public class TestSitesViewModel
{
    //Eventually this will be added to a base ViewModel to get rid of
    //the ViewBag dependencies
    [Display(Name = "Web Page Title")]
    public string WebPageTitle { get; set; }

    //PUBLIC DATA MEMBER WON'T WORK
    //IT NEEDS TO BE PROPERTY AS DECLARED ABOVE
    //public string WebPageTitle;

    public IEnumerable<MVCWeb.MyEntities.Site> Sites;

    //Other Entities, IEnumberables, or properties go here
    //Not important for this example
}

问题第二部分是访问元数据在一个IEnumerable变量。我叫申报临时headerMetadata变量,使用它,而不是试图通过了IEnumerable

2nd part of problem was accessing the Metadata in an IEnumerable variable. I declared temporary variable called headerMetadata and used it instead of trying to access the properties through the IEnumerable

@{var headerMetadata = Model.Sites.FirstOrDefault();}
<tr>
    <th>            
        @Html.DisplayNameFor(model => headerMetadata.Abbreviation)
    </th>
    <th>
        @Html.DisplayNameFor(model => headerMetadata.DisplayName)
    </th>
    ...

这不乞求什么时候headerMetadata变量超出作用域的问题吗?它是适用于整个视图或者是仅限于HTML表标签?我想这是其它日期的另一个问题。

This does beg the question of when does headerMetadata variable go out of scope? Is it available for the entire view or is it limited to the html table tags? I suppose that's another question for another date.

这篇关于MVC和Entity Framework Html.DisplayNameFor与复合视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆