在ASP.NET MVC 2中与SelectList苦苦挣扎 [英] Struggling with SelectList in ASP.NET MVC 2

查看:58
本文介绍了在ASP.NET MVC 2中与SelectList苦苦挣扎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的模型:

I have a model that looks something like this:

public class SampleModel
{
    public static SampleModel Create()
    {
        return new SampleModel
        {
            Boolean = true,
            // set several more properties...
            Colors = new SelectList(new[] { "Red", "Green", "Blue" }, "Green")
        };
    }

    public bool Boolean { get; set; }
    // define several more properties...
    public SelectList Colors { get; set; }
}

我让ASP.NET MVC自动使用属性 Html.DisplayForModel()来显示属性,并使用 Html.EditorForModel()来编辑属性.

I'm letting ASP.NET MVC automatically scaffold the properties using Html.DisplayForModel() for my Details view and Html.EditorForModel() for my Edit view.

结果:

  • 编辑"视图效果很好.颜色显示为包含三个项目(红色,绿色和蓝色)的菜单,默认情况下选择绿色.

  • The Edit view works great. Colors shows up as a menu with three items (Red, Green, and Blue), and Green is selected by default.

但是,对于详细信息"视图,我得到"False True False",这显然是菜单的每个项目的 IsSelected 值的列表.那绝对不是我想要的.我希望它只显示绿色".

For the Details view, however, I get "False True False", which is apparently a list of the IsSelected value for each item of the menu. That is definitely not want I want. I would like it to just display "Green".

另一个问题是,如果我尝试在控制器中执行 UpdateModel(sampleModel),则会收到错误消息:没有为此对象定义无参数的构造函数."大概是因为 Colors SelectList ,并且没有 SelectList 的无参数构造函数,所以它无法完成绑定.

A further problem is that, if I try to do UpdateModel(sampleModel) in my controller, I get the error, "No parameterless constructor defined for this object." This is presumably because Colors is a SelectList, and there is no parameterless constructor for SelectList, so it can't complete the binding.

所以,我认为我理解了问题所在,但是对于这种情况,我真的可以使用一个好的解决方案,当您使用带有外键的模型对象时,这种解决方案似乎很有用.

So, I think I understand what the problems are, but I could really use a good solution for this scenario, which seems to come up a lot when you have models objects with foreign keys.

一些问题:

  1. 如何获取 Colors 作为编辑"视图的菜单,但在详细信息"视图中仅显示为简单的字符串值(例如绿色")?
  2. 在更新包含 SelectList 的模型时,如何防止出现无参数构造函数"错误?
  3. 如果我的Model/ViewModel实际上包含一个外键 ColorId ,而不是 Colors ,那么获取详细信息"视图以显示其名称的最佳做法是什么当前颜色和编辑"视图以显示一个菜单,其中包含数据库的 Color 表中列出的所有颜色名称.如果有帮助,我正在使用LinqToSql(SqlMetal)生成我的模型类.
  1. How do I get Colors to show up as a menu for the Edit view but as just a simple string value (e.g., "Green") in the Details view?
  2. How can I prevent a "no parameterless constructor" error when updating a model that contains a SelectList?
  3. If my Model/ViewModel actually contained a foreign key, ColorId, instead of Colors, what would be the best practice for getting the Details view to show the name of the current color and the Edit view to show a menu containing all the color names listed in the Color table of my database. If it helps, I'm using LinqToSql (SqlMetal) to generate my model classes.

推荐答案

您的所有问题都源于您将SelectList作为模型属性公开的事实.根据实际情况,我可能会做类似的事情:

Your problems all stem from the fact that you're exposing a SelectList as a model property. Depending on the exact situation, I'd probably do something like:

  1. 创建3个单独的模型类:一个由LinqToSql对象(或它们周围的包装器)组成的域"模型和两个视图模型,一个用于详细信息"视图,一个用于编辑"视图.

  1. Create 3 separate model classes: one "domain" model consisting of your LinqToSql objects (or wrappers around them) and two view models, one for the Details view and one for the Edit view.

详细信息"视图模型应包含当前选择的颜色名称.详细信息"操作将使用域对象中的FK关系或通过手动查找所选颜色的详细信息来设置此属性.

The Details view model should contain the currently selected color name. The Details action would set this property, either using the FK relationship in your domain objects or by manually looking up the details for the selected color.

编辑"视图模型应包含所选颜色ID的属性.它还应包含可用颜色选项的选择列表,并将所选值设置为当前所选ID.

The Edit view model should contain a property for the selected color's ID. It should also contain a select list of available color options, with the selected value set to the currently selected ID.

处理表单发布的编辑"操作应接受编辑"视图模型的实例,并将这些更改映射到域模型上.由于所选颜色仅显示一个简单的属性,而不是选择列表,因此很容易验证并映射回业务对象.

The Edit action that handles the form post should accept an instance of the Edit view model and should map those changes onto the domain model. Since the selected color is exposed a simple property, rather than a select list, it's easy to validate and map back onto the business object.

如果您的情况很简单,则可以为视图"和编辑"使用单一视图模型,甚至可以将域对象直接传递到详细信息"视图.无论如何,只要从模型中删除SelectList并将所选颜色显示为简单ID,就可以了.

If your situation is very simple you might be able to get away with a single view model for both View and Edit, or even passing the domain object directly to the Details view. In any case, as long as you remove the SelectList from your model and expose the selected color as a simple ID then you should be OK.

这篇关于在ASP.NET MVC 2中与SelectList苦苦挣扎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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