Again about custom templates
Some times ago I asked about problem with custom template.
I find solution to use not-strongly typed view for custom template (for double
typed properties):
@{
string id = ViewData.TemplateInfo.GetFullHtmlFieldId("");
string name = ViewData.TemplateInfo.GetFullHtmlFieldName("");
st开发者_高级运维ring value = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}", ViewData.ModelMetadata.Model);
}
<input type="text" id="@id" name="@name" value="@value" />
But I can't find information about how method from TemplateInfo gets the name and id? What parametr I should pass to them, if property is complex-type object?
They default to HtmlFieldPrefix
public string GetFullHtmlFieldName(string partialFieldName)
{
return (this.HtmlFieldPrefix + "." + (partialFieldName ?? string.Empty)).Trim(new char[] { '.' });
}
public string GetFullHtmlFieldId(string partialFieldName)
{
return HtmlHelper.GenerateIdFromName(this.GetFullHtmlFieldName(partialFieldName));
}
Inside your partial view, you can even set the HtmlFieldPrefix
to something from your view model
ViewData.TemplateInfo.HtmlFieldPrefix = Model.MyPrefix; // MOdel refers to your view model instance
On a related note, take a look here: http://btburnett.com/2011/03/correcting-mvc-3-editorfor-template-field-names-when-using-collections.html
精彩评论