ASP.NET MVC的部分观点:输入名称prefixes [英] ASP.NET MVC partial views: input name prefixes
问题描述
假设我有喜欢视图模型
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; }
}
在认为我可以使部分用
<% Html.RenderPartial("AnotherViewModelControl", Model.Child) %>
在部分我会做
<%= Html.TextBox("Name", Model.Name) %>
or
<%= Html.TextBoxFor(x => x.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.
如何让我的局部视图自动识别所需的preFIX?我可以把它作为一个参数,但这样太不方便了。当我想例如递归使其这更是雪上加霜。有没有一种方法来呈现一个preFIX,或者甚至更好的局部视图,与调用的lambda前pression自动reconition让
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) %>
将自动添加正确的孩子。 preFIX到生成的名称/ ID字符串?
will automatically add correct "Child." prefix to the generated name/id strings?
我可以接受任何解决方案,其中包括3-第三方视图引擎和库 - 实际上我用星火视图引擎(我解决利用其宏问题)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&LT; T&GT; {公共字符串preFIX; T型车; }。但我宁愿preFER一些现有/认可的解决方案。
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.
更新:有没有答案<一个类似的问题href=\"http://stackoverflow.com/questions/955371/partial-view-with-parametrized-$p$pfix-for-controls-names\">here.
推荐答案
您可以通过此扩展HTML辅助类:
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") %>
,你会看到一切正常!
and you will see everything is ok!
这篇关于ASP.NET MVC的部分观点:输入名称prefixes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!