用可重复使用的DropDownList编辑模板业务对象 [英] Reusable editor template with DropDownList for business objects

查看:147
本文介绍了用可重复使用的DropDownList编辑模板业务对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC3剃刀意见,并希望构建可重用DropDownLists好我的班,但经过一番搜索我没有发现,执行正是我需要的...

I'm using MVC3 with Razor views and would like to build reusable DropDownLists for several of my classes, but after much searching I have not found an example that performs exactly how I need it...

在这个例子中,我有两个班是这样的: -

For this example I have two classes like this:-

public class Person
{
  public int ID { get; set; }
  public string Name { get; set; }
  public Group Group { get; set; }
}

public class Group
{
  public int ID { get; set; }
  public string Name { get; set; }
}

我有一个人工作的控制器/视图。该视图有一个DropDownListFor控制:

I have a working Controller/View for Person. The view has a DropDownListFor control:

@model Person

...

@Html.DropDownListFor(o => o.Group.ID, (ViewData["groups"] as SelectList))

视图中直接使用了Person类,不是中介的模式,因为我还没有找到一个令人信服的理由,在这个阶段从另一个抽象的。

The view uses the Person class directly, not an intermediary model, as I haven't found a compelling reason to abstract one from the other at this stage.

以上作品不错...控制器我抓住从Group.ID的人从视图返回的值,看看它,并设置Person.Group到结果。工作,但效果并不理想。

The above works fine... in the controller I grab the value from Group.ID in the Person returned from the view, look it up, and set Person.Group to the result. Works, but not ideal.

我发现这里的粘合剂:<一href=\"http://stackoverflow.com/questions/5657826/mvc-dropdownlist-values-posted-to-model-arent-bound\">MVC贴到模型的DropDownList值未绑定,将工作这一点对我来说,但我没有得到该工作尚未...因为它只是似乎真的有用的,如果我可以重复使用。

I've found a binder here: MVC DropDownList values posted to model aren't bound that will work this out for me, but I haven't got that working yet... as it only really seems useful if I can reuse.

我想要做的是有这样的事情在一个模板: -

What I'd like to do is have something like this in a template:-

@model Group

@Html.DropDownListFor(o => o.Group.ID, (ViewData["groups"] as SelectList))

而在这样的视图中使用它: -

And use it in a view like this:-

@Html.EditorFor(o => o.Group)

不过上面似乎没有工作...上面EditorFor线插入全班编辑器(如Group.Description以及文本框)......而不是插入我的团一个DropDownList列出

However the above doesn't seem to work... the above EditorFor line inserts editors for the whole class (e.g. a textbox for Group.Description as well)... instead of inserting a DropDownList with my groups listed

我有一个下查看名为Group.cshtml文件上面的模板/共享/ EditorTemplates

I have the above template in a file called Group.cshtml under Views/Shared/EditorTemplates

如果这个工作,那么当一个类有型集团的财产,这DropDownList的编辑器将默认使用(或至少如果由一些属性指定)

If this worked, then whenever a class has a property of type Group, this DropDownList editor would be used by default (or at least if specified by some attribute)

在此先感谢提供任何意见...

Thanks in advance for any advice provided...

推荐答案

您可以创建一个下拉列表,用户控件处理这个问题。在共享文件夹中创建一个名为EditorTemplates文件夹,将用户控件存在。 MVC,按照惯例,看起来任何编辑模板的共享/ EditorTemplates。您可以覆盖的地方查找编辑模板,但我不会在这里去那。

You can create a drop down list user control to handle this. Under your Shared folder create a folder called EditorTemplates and place your user control there. MVC, by convention, looks in the Shared/EditorTemplates for any editor templates. You can override where it looks for the editor templates but I won't go in to that here.

一旦你的用户控件创建,你需要用UIHint属性来装点相应的属性告诉引擎哪个编辑器应该为该属性使用。

Once you have your user control created, you'll need to decorate the appropriate property with the "UIHint" attribute to tell the engine what editor it should use for that property.

下面的是一个样本实现。

Following would be a sample implementation.

在共享/文件夹EditorTemplates用户控件(_GroupsDropDown.cshtml在这种情况下)看起来像:

In the Shared/EditorTemplates folder your user control (_GroupsDropDown.cshtml in this case) would look like:

@model Group

@Html.DropDownListFor(o => o.ID, (ViewData["groups"] as SelectList))

修改组属性在Person添加UIHint属性如下:

Modify the Group property in the Person to add the UIHint attribute as follows:

**[UIHint("_GroupsDropDown")]**
public Group Group { get; set; }

在你的控制器,你需要

ViewData["groups"] = new SelectList(<YourGroupList>, "ID", "Name");

一旦你有了上述code,你可以像你想要使用EditorFor语法。

Once you have the above code you can use the EditorFor syntax like you desire.

希望这有助于。

这篇关于用可重复使用的DropDownList编辑模板业务对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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