reusable checkbox partial view

你。 提交于 2019-12-01 23:33:09

You need to pass the prefix to the partial view so the elements are correctly named

@if (Model.Filters.IsReturnsOptionsAvailable)
{
  Html.RenderPartial("_CheckBoxFilterPartial", Model.clmReturnOptions, new ViewDataDictionary
  {
    TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "clmReturnOptions" }
  })
}

You can also write a custom html helper to make this a little easier

public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper, 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 use as

@Html.PartialFor(m => m.clmReturnOptions, "_CheckBoxFilterPartial")

Create an interface that provides access to the IList<CheckBoxModel>

public interface ICheckBoxList
{
    IList<CheckBoxModel> CheckBoxes { get; set; }
}

Have CheckBoxListModel implement that interface

public class CheckBoxListModel:ICheckBoxList
{...

Your _CheckBoxListPartial.cshtml partial view will use the new interface as its model

@model MySite.Areas.Reports.Models.ViewModels.ICheckBoxList

and change your EditorFor to

@Html.EditorFor(x => x.CheckBoxes)

I couldn't find the code in your question that showed how you are including the _CheckBoxListPartial partial view, but you would simply pass the clmReturnOptions property of the ViewModel (ReportFilterViewModel or otherwise) instead of the entire model.

And you should be good to go.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!