可重用的编辑器模板,具有用于业务对象的DropDownList [英] Reusable editor template with DropDownList for business objects

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

问题描述

我正在使用MVC3的Razor视图,并希望为我的几个类创建可重用的DropDownLists,但经过多次搜索,我还没有找到一个正确执行我需要的例子。



对于这个例子,我有两个这样的类: -

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

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

我有一个工作的Controller / View for Person。该视图有一个DropDownListFor控件:

  @model Person 

...

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

该视图直接使用Person类,而不是中间模型,因为我没有发现一个引人注目的理由在这个阶段从另一个抽象。



上述工作正常...在控制器中,我从视图中返回的Person中获取Group.ID中的值,查找并将Person.Group设置为结果。工作,但不是理想。



我在这里找到了一个活页夹:发布到模型的MVC DropDownList值没有绑定,这将为我工作,但是我还没有工作,因为它只是如果我可以重用,真的好像很有用。



我想做的是在模板中有这样的东西: -

  @model Group 

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

并在如下视图中使用它: -

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

然而,上述似乎不起作用...上面的EditorFor线插入整个类的编辑器(例如Group.Description的文本框)...而不是在我的组中插入一个DropDownList列出



我将上述模板放在名为Gr的文件中视图/共享/编辑器模板下的oup.cshtml



如果这样工作,那么每当类具有类型为Group的属性时,默认情况下将使用该DropDownList编辑器至少如果某些属性指定)



提前感谢提供的任何建议...

解决方案

您可以创建一个下拉列表用户控件来处理此问题。在共享文件夹下创建一个名为EditorTemplates的文件夹,并将用户控件放在那里。按照惯例,MVC在Shared / EditorTemplates中查找任何编辑器模板。您可以覆盖编辑器模板的位置,但我不会在这里进入。



创建用户控件后,您需要使用UIHint属性来装饰相应的属性,以告知引擎应该为该属性使用什么编辑器



以下是一个示例实现。



在Shared / EditorTemplates文件夹中,您的用户控件(_GroupsDropDown。在这种情况下,cshtml)将如下所示:

  @model Group 

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

修改组添加UIHint属性的属性如下:

  ** [UIHint(_ GroupsDropDown)] ** 
public Group Group {get;组;

在您的控制器中,您需要

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

一旦你拥有上面的代码,你可以像你想要的那样使用EditorFor语法。



希望这有帮助。


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

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

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.

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.

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)

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

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

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

解决方案

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.

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.

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

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

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

In your controller you would need

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

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

Hope this helps.

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

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