Asp.Net MVC 3编辑器动态属性 [英] Asp.Net MVC 3 Editor for dynamic property

查看:143
本文介绍了Asp.Net MVC 3编辑器动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在努力让主编模板与动态属性工作 - 无济于事。也许你们中的一个可以帮助我们。

We have been trying to get the Editor-Template to work with a dynamic property - to no avail. Maybe one of you can help us.

下面大致是我们班:

public class Criterion
{
    ...
    public string Text { get; set; }
    public dynamic Value { get; set; }
    public Type Type { get; set; }
    ...
}

我们的剃须刀观点得到了模型containg节的列表,每个包含在其标准的列表。 (我们得到这些相关信息在运行时)所有这些标准应显示在编辑模式 - 对他们的实际类型:(节选)

Our razor view gets a model containg a list of sections which each contains a list of criteria in it. (We get these infos at runtime.) All these criteria should be displayed in edit mode - regarding their actual type: (excerpt)

@for (int i = 0; i < model.Sections.Count(); i++)
{
    for (int j = 0; j < model.Sections[i].Criteria.Count(); j++)
    {
        var criterion = model.Sections[i].Criteria[j];
        var type = criterion.Type.Name;
        var name = "Sections[" + i + "].Criteria[" + j + "].Value";
        var criterionDisplayName = criterion.Text;
        <label for="Sections_@(i)__Criteria_@(j)__Value">@criterionDisplayName</label>
        @Html.Editor(name, type)
    }
}

这确实例如显示一个复选框正确,但它不使用的值来正确地设置的复选框的状态(检查,如果criterion.Value为真)。同去的其他类型,如整数
(它确实是POST请求后,正确填写表格,但那是因为MVC使用临时模型来重新创建用户的输入。)

This does display for instance a checkbox correctly, but it does not use the value to set the checkbox status correctly (checked if the criterion.Value is true). Same goes for other types, like ints. (It does fill the form correctly after a POST request, but that is because MVC uses a temporary model to recreate the users input.)

正如我们已经尝试和研究:它甚至有可能使用具有类型的属性编辑器模板动态?如果是的话 - 我们怎样才能使它发挥作用? (我们不希望根据可能的类型辨别。我们希望有MVC框架使用基于实际类型正确的编辑模板。)

As much as we have tried and researched: Is it even possible to use the Editor template with properties of type dynamic? If yes - how can we make it work? (We would not like to discern according to the possible type. We would like to have the MVC framework to use the right Editor template based on the actual type.)

推荐答案

动态不很好地与ASP.NET MVC适合该法案。他们提醒我关于 ViewBag 我恨 ViewBag 从我的身体非常深刻的面料。所以我会采取不同的方法。

Dynamics don't fit the bill nicely with ASP.NET MVC. They remind me about ViewBag and I hate ViewBag from the very deep fabrics of my body. So I would take a different approach.

让我们来举个例子如下模型:

Let's take for example the following model:

public class Criterion
{
    public string Text { get; set; }
    public object Value { get; set; }
}

值可以是您要处理任何类型的。

Value could be any type that you wish to handle.

现在你可以有一个控制器,它填充这个模型:

Now you could have a controller which populates this model:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new[]
        {
            new Criterion { Text = "some integer", Value = 2 },
            new Criterion { Text = "some boolean", Value = true },
            new Criterion { Text = "some string", Value = "foo" },
        };
        return View(model);
    }
}

,然后相应的视图:

and then a corresponding view:

@model IList<Criterion>

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        <div>
            @Html.LabelFor(x => x[i], Model[i].Text)
            @Html.EditorFor(x => x[i].Value, "Criterion_" + Model[i].Value.GetType().Name)
        </div>
    }

    <button type="submit">OK</button>
}

现在对于要处理,你可以定义一个相应的编辑器模板的每个类型:

Now for each type that you want to handle you could define a corresponding editor template:

〜/查看/共享/ EditorTemplates / Criterion_String.cshtml

@model string
@Html.TextBoxFor(x => x)

〜/查看/共享/ EditorTemplates / Criterion_Boolean.cshtml

@model bool
@Html.CheckBoxFor(x => x)

〜/查看/共享/ EditorTemplates / Criterion_Int32.cshtml

@model int
@{
    var items = Enumerable
        .Range(1, 5)
        .Select(x => new SelectListItem 
        { 
            Value = x.ToString(), 
            Text = "item " + x 
        });
}

@Html.DropDownListFor(x => x, new SelectList(items, "Value", "Text", Model))

显然显示视图中的这种模式只是第一步。我想你会希望得到用户的一些处理POST控制器动作进入后面的值。在这种情况下,一些小的修改是必要的。我们需要添加一个自定义模型粘结剂,将能够实例在运行时正确的类型,并包括具体类型作为每一行的隐藏字段。我已经显示在 为例这篇文章 。在这个例子中,我用一个基类,而不是用的对象直接键入工作还要注意。

Obviously displaying this model in the view is only the first step. I suppose that you will want to get the values that the user entered back in the POST controller action for some processing. In this case some small adaptations are necessary. We need to add a custom model binder that will be able to instantiate the correct type at runtime and include the concrete type as hidden field for each row. I have already shown an example in this post. Also notice in this example that I used a base class instead of directly working with the object type.

这篇关于Asp.Net MVC 3编辑器动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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