2013-12-19 82 views
0

我有以下型號:添加前綴EditorFor所有元素

public class CaseFormViewModel 
{ 
    public int ID { get; set; } 
    public int AppID { get; set; } 

    public CaseGeneralFormViewModel General { get; set; } 

    public CaseMedicalFormViewModel Medical { get; set; } 

    public CaseLegalFormViewModel Legal { get; set; } 

    public CaseCommentsFormViewModel Comments { get; set; } 

    public List<UploadedDocumentModel> Attachments { get; set; } 

    public string AzureStorage { get; set; } 

} 

public class CaseGeneralFormViewModel : CaseGeneralViewModelBase 
{ 
    [Required] 
    public new string PatientName { get; set; } 

我也有模型CaseFormViewModel一個視圖,且具有與模型CaseGeneralFormViewModel的局部視圖。視圖加載此局部視圖:

@Html.Partial("PartialCaseGeneralForm", Model.General) 

局部視圖具有以下字符串:

@Html.ValidationMessageFor(model => model.PatientName) 

然後,當我張貼此視圖到控制器方法:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult UpdateCase(int AppID, CHFN.Models.CaseFormViewModel model) 
    { 

model.General是空值。我明白爲什麼,因爲PatientName應該有id =「General.PatientName」而不是id =「PatientName」,但是如何將該前綴添加到頁面上的所有元素?

我看到一些能力做到這一點:修改後控制器的方法:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult UpdateCase(int AppID, CaseGeneralFormViewModel general) 
    { 

但首先在所有,代碼是髒(內部類的結構被破壞),其次 - 任何類(即CaseGeneralFormViewModel)可以具有2個屬性相同的類(即

public Class1 Prop1 { get; set; } 
public Class1 Prop2 { get; set; } 

)。如何解決這個問題 - 我不知道....

回答

1

解決辦法是:

創建用於部分的一個推廣方法:

public static MvcHtmlString PartialFor<TModel, TProperty>(
     this HtmlHelper<TModel> helper, 
     System.Linq.Expressions.Expression<Func<TModel, TProperty>> expression, 
     string partialViewName) 

{ 
     var name = ExpressionHelper.GetExpressionText(expression); 
     var 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); 
} 

及用途:

@Html.PartialFor(m=>m.General,"PartialCaseGeneralForm") 
0

在asp.net core mvc

public static class HtmlHelperPartialExtensions 
{ 
    public static IHtmlContent PartialWithPrefix(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix) 
    { 
     var htmlFieldPrefix = (string.Empty.Equals(prefix) ? "." : "") + prefix; 
     return htmlHelper.Partial(partialViewName, model, new ViewDataDictionary(htmlHelper.ViewData) { TemplateInfo = { HtmlFieldPrefix = htmlFieldPrefix } }); 
    } 

    public static Task<IHtmlContent> PartialWithPrefixAsync(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix) 
    { 
     var htmlFieldPrefix = (string.Empty.Equals(prefix) ? "." : "") + prefix; 
     return htmlHelper.PartialAsync(partialViewName, model, new ViewDataDictionary(htmlHelper.ViewData) {TemplateInfo = {HtmlFieldPrefix = htmlFieldPrefix}}); 
    } 

    public static IHtmlContent PartialWithPrefixFor<TModel, TProperty>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string partialViewName) 
    { 
     string prefix = ExpressionHelper.GetExpressionText(expression); 
     object model = ExpressionMetadataProvider.FromLambdaExpression(expression, helper.ViewData, helper.MetadataProvider).Model; 
     return PartialWithPrefix(helper, partialViewName, model, prefix); 
    } 

    public static Task<IHtmlContent> PartialWithPrefixForAsync<TModel, TProperty>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string partialViewName) 
    { 
     string prefix = ExpressionHelper.GetExpressionText(expression); 
     object model = ExpressionMetadataProvider.FromLambdaExpression(expression, helper.ViewData, helper.MetadataProvider).Model; 
     return PartialWithPrefixAsync(helper, partialViewName, model, prefix); 
    } 
} 
相關問題