Attempting to make templates out of common scenarios in a UI in MVC and not able to arrive at a solution.
currently a simplified version of the code looks like the following
View model
public class Users
{
[Display(Name = "FirstName", ResourceType = typeof(ModelResources))]
public string FirstName { get; set; }
[Display(Name = "LastName", ResourceType = typeof(ModelResources))]
public string LastName { get; set; }
}
Partial razor view (_TextInputPartial.chtml)
@model object
<div class="wrapper">
<div class="label-wrap">
@Html.LabelFor(m => m)
</div>
<div class="input-wrap">
@Html.TextBoxFor(m => m)
</div>
<div class="validation-wrap">@Html.ValidationMessageFor(m => m)</div>
</div>
Html extension method for rendering the partial
public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
{
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("~/Areas/Shared/_TextInputPartial.cshtml", model, viewData);
}
Usage in the parent view
@using (Html.BeginForm())
{
Html.PartialFor(m => m.FirstName);
<br />
<input type="submit" value="Submit" />
}
Currently this contraption doesn't render anything, when stepping through the code it appears that the partial view gets a value but looses all metadata about the property. What am i doing wrong here? Or is there a better way of doing this?