MVC动态查看数据和动态视图 [英] MVC Dynamic View Data and Dynamic Views

查看:111
本文介绍了MVC动态查看数据和动态视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传统上,我已经建立了使用视图模型MVC应用与数据注释属性,动态地呈现使用编辑器模板的意见。一切的伟大工程,它确实减少了需要我来构建新的视图的时间。我的要求,最近已经改变。现在,我不能定义在设计时视图模型。这将在视图呈现的特性是在基于业务规则的运行时间决定。此外,对于那些性能验证规则可以在运行时决定为好。 (可在我看来,需要未在我的域模型需要一个领域,根据业务规则)。此外,要呈现不知道,直到运行时的属性集 - 用户A可以编辑从模型6的属性,而用户B可以编辑9性质

Traditionally, I have built MVC applications using view models with Data Annotations attributes, and I dynamically render the views using editor templates. Everything works great, and it really cuts down on the time it takes me to build new views. My requirements have recently changed. Now, I can't define the view model at design time. The properties that will be rendered on the view are decided at run time based on business rules. Also, the validation rules for those properties may be decided at run time as well. (A field that is not required in my domain model, may be required in my view based on business rules). Also, the set of properties that will be rendered is not known until run time - User A may edit 6 properties from the model, while user B may edit 9 properties.

我想知道是否有可能创建将业务规则提供自己的元数据,对于像属性名称和值的集合非类型化的视图模型一个模型元数据提供商。有没有人解决了这个问题?

I am wondering if it is possible to create a model metadata provider that will supply my own metadata from business rules for an untyped view model like a collection of property names and values. Has anyone solved this problem?

推荐答案

我通过创建一个更复杂的模型,并使用自定义编辑模板,使模型呈现看起来像一个典型的编辑器,但使用解决了类似的问题,动态字段信息:

I solved a similar problem by creating a more complex model, and using a custom editor template to make the model be rendered to look like a typical editor, but using the dynamic field information:

public class SingleRowFieldAnswerForm
{
    /// <summary>
    /// The fields answers to display.
    /// This is a collection because we ask the MVC to bind parameters to it,
    /// and it could cause issues if the underlying objects were being recreated
    /// each time it got iterated over.
    /// </summary>
    public ICollection<IFieldAnswerModel> FieldAnswers { get; set; } 
}

public interface IFieldAnswerModel
{
    int FieldId { get; set; }

    string FieldTitle { get; set; }

    bool DisplayAsInput { get; }

    bool IsRequired { get; }

    bool HideSurroundingHtml { get; }
}

// sample implementation of IFieldAnswerModel
public class TextAreaFieldAnswer : FieldAnswerModelBase<TextAreaDisplayerOptions>
{
    public string Answer { get; set; }
}

EditorTemplates / SingleRowFieldAnswerForm.cshtml:

EditorTemplates/SingleRowFieldAnswerForm.cshtml:

@helper DisplayerOrEditor(IFieldAnswerModel answer)
{
    var templateName = "FieldAnswers/" + answer.GetType().Name;
    var htmlFieldName = string.Format("Answers[{0}]", answer.FieldId);

    if (answer.DisplayAsInput)
    {
        @Html.EditorFor(m => answer, templateName, htmlFieldName)
        // This will display validation messages that apply to the entire answer. 
        // This typically means that the input got past client-side validation and
        // was caught on the server instead.
        // Each answer's view must also produce a validation message for
        // its individual properties if you want client-side validation to be
        // enabled.
        @Html.ValidationMessage(htmlFieldName)
    }
    else
    {
        @Html.DisplayFor(m => answer, templateName, htmlFieldName)
    }
}
<div class="form-section">
    <table class="form-table">
        <tbody>
            @{
                foreach (var answer in Model.FieldAnswers)
                {
                    if (answer.HideSurroundingHtml)
                    {
                        @DisplayerOrEditor(answer)
                    }
                    else
                    {
                        var labelClass = answer.IsRequired ? "form-label required" : "form-label";
                        <tr>
                            <td class="@labelClass">
                                @answer.FieldTitle:
                            </td>
                            <td class="form-field">
                                <div>
                                    @DisplayerOrEditor(answer)
                                </div>
                            </td>
                        </tr>
                    }
                }
            }
        </tbody>
    </table>
</div>

所以我填充我 SingleRowFieldAnswerForm 的一系列答案车型。每个答案的模型类型都有它自己的编辑模板,让我来定制不同类型的动态特性应如何显示。例如:

So I populate my SingleRowFieldAnswerForm with a series of answer models. Each answer model type has its own editor template, allowing me to customize how different types of dynamic "properties" should be displayed. For example:

// EditorTemplates/FieldAnswers/TextAreaFieldAnswer.cshtml

@model TextAreaFieldAnswer

@{
    var htmlAttributes = Html.GetUnobtrusiveValidationAttributes("Answer", ViewData.ModelMetadata);
    // add custom classes that you want to apply to your inputs.
    htmlAttributes.Add("class", "multi-line input-field");
}
@Html.TextAreaFor(m => m.Answer, Model.Options.Rows, 0, htmlAttributes)
@Html.ValidationMessage("Answer")

接下来的棘手的部分是,当你将这些信息发送给服务器,它本身并没有认识到构建哪种类型的 IFieldAnswerModel ,所以你不能随便在参数列表绑定 SingleRowAnswerForm 。相反,你必须做这样的事情:

The next tricky part is that when you send this information to the server, it doesn't inherently know which type of IFieldAnswerModel to construct, so you can't just bind the SingleRowAnswerForm in your arguments list. Instead, you have to do something like this:

public ActionResult SaveForm(int formId)
{
    SingleRowAnswerForm form = GetForm(formId);
    foreach (var fieldAnswerModel in form.FieldAnswers.Where(a => a.DisplayAsInput))
    {
        // Updating this as a dynamic makes sure all the properties are bound regardless
        // of the runtime type (since UpdateModel relies on the generic type normally).
        this.TryUpdateModel((dynamic) fieldAnswerModel, 
            string.Format("Answers[{1}]", fieldAnswerModel.FieldId));
        }
    ...

由于您使用的每个动态特性值绑定到提供MVC,它可以绑定在每一个每个答案类型的属性没有任何困难。

Since you provided MVC with each dynamic "property" value to bind to, it can bind each of the properties on each answer type without any difficulty.

显然,我省略了很多细节,比如如何产生的回答模型摆在首位,但希望这让你在正确的轨道上。

Obviously I've omitted a lot of details, like how to produce the answer models in the first place, but hopefully this puts you on the right track.

这篇关于MVC动态查看数据和动态视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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