尝试在MVC的UI中使用常见方案制作模板,但无法获得解决方案 .

目前,代码的简化版本如下所示

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);
    
<input type="submit" value="Submit" /> }

目前这个装置没有渲染任何东西,当单步执行代码时,部分视图会获得一个值,但会丢失有关该属性的所有元数据 . 我在这做错了什么?或者有更好的方法吗?