ASP.NET MVC 部分视图:输入名称前缀 [英] ASP.NET MVC partial views: input name prefixes

查看:26
本文介绍了ASP.NET MVC 部分视图:输入名称前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有类似的 ViewModel

Suppose I have ViewModel like

public class AnotherViewModel
{
   public string Name { get; set; }
}
public class MyViewModel
{
   public string Name { get; set; }
   public AnotherViewModel Child { get; set; }
   public AnotherViewModel Child2 { get; set; }
}

在视图中,我可以使用

In the view I can render a partial with

<% Html.RenderPartial("AnotherViewModelControl", Model.Child) %>

在部分我会做

<%= Html.TextBox("Name", Model.Name) %>
or
<%= Html.TextBoxFor(x => x.Name) %>

然而,问题是两者都会呈现 name="Name" 而我需要 name="Child.Name" 以便模型绑定器正常工作.或者,当我使用相同的局部视图呈现第二个属性时,name="Child2.Name".

However, the problem is that both will render name="Name" while I need to have name="Child.Name" in order for model binder to work properly. Or, name="Child2.Name" when I render the second property using the same partial view.

如何让我的局部视图自动识别所需的前缀?我可以将它作为参数传递,但这太不方便了.例如,当我想递归渲染它时,情况就更糟了.有没有办法用前缀渲染局部视图,或者更好的是,通过调用 lambda 表达式的自动重组,以便

How do I make my partial view automatically recognize the required prefix? I can pass it as a parameter but this is too inconvenient. This is even worse when I want for example to render it recursively. Is there a way to render partial views with a prefix, or, even better, with automatic reconition of the calling lambda expression so that

<% Html.RenderPartial("AnotherViewModelControl", Model.Child) %>

会自动添加正确的孩子".生成的名称/ID 字符串的前缀?

will automatically add correct "Child." prefix to the generated name/id strings?

我可以接受任何解决方案,包括第 3 方视图引擎和库 - 我实际上使用 Spark 视图引擎(我使用其宏解决"了问题)和 MvcContrib,但没有在那里找到解决方案.XForms、InputBuilder、MVC v2 - 任何提供此功能的工具/洞察力都会很棒.

I can accept any solution, including 3-rd party view engines and libraries - I actually use Spark View Engine (I "solve" the problem using its macros) and MvcContrib, but did not find a solution there. XForms, InputBuilder, MVC v2 - any tool/insight that provide this functionality will be great.

目前我正在考虑自己编写代码,但这似乎是在浪费时间,我不敢相信这些琐碎的东西还没有实现.

Currently I think about coding this myself but it seems like a waste of time, I can't believe this trivial stuff is not implemented already.

可能存在很多手动解决方案,欢迎所有这些解决方案.例如,我可以强制我的部分基于 IPartialViewModel<T>{ 公共字符串前缀;T型;}.但我更喜欢一些现有/批准的解决方案.

A lot of manual solutions may exists, and all of them are welcome. For example, I can force my partials to be based off IPartialViewModel<T> { public string Prefix; T Model; }. But I'd rather prefer some existing/approved solution.

更新:有一个没有答案的类似问题这里.

UPDATE: there's a similar question with no answer here.

推荐答案

您可以通过以下方式扩展 Html helper 类:

You can extend Html helper class by this :

using System.Web.Mvc.Html


 public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper, System.Linq.Expressions.Expression<Func<TModel, TProperty>> expression, string partialViewName)
    {
        string name = ExpressionHelper.GetExpressionText(expression);
        object model = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
        var viewData = new ViewDataDictionary(helper.ViewData)
        {
            TemplateInfo = new System.Web.Mvc.TemplateInfo
            {
                HtmlFieldPrefix = name
            }
        };

        return helper.Partial(partialViewName, model, viewData);

    }

并像这样简单地在您的视图中使用它:

and simply use it in your views like this :

<%= Html.PartialFor(model => model.Child, "_AnotherViewModelControl") %>

你会看到一切正常!

这篇关于ASP.NET MVC 部分视图:输入名称前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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