asp.net-mvc - 一个 View 中的两个模型,仅验证其中一个

标签 asp.net-mvc asp.net-mvc-3 view model

我有一个包含 ParentModel 的 View ,其中包含 2 个模型。 我只想验证一个或另一个的字段。不是都。 假设我有:

public ParentModel{
  public BlueUser BlueUser {get; set;}
  public GreenUser GreenUser {get; set;}
}

所以用户选择他是 GreenUser 还是 BlueUser。每种类型的用户都有不同的字段,根据用户选择的用户类型,我只想验证该特定类型用户的字段。 如何实现?

编辑: 两种形式/模型必须在同一个 View 上,也许有一些 JQuery 或 Partials?

最佳答案

HtmlHelper<BlueUser> 创建一个表单另一个是HtmlHelper<GreenUser> ,并让它们发布到不同的操作。

首先, View 模型是:

@model ParentModel

然后使用HtmlHelperFor为每个子模型创建一个 HtmlHelper:

@{
   var blueHtml = Html.HtmlHelperFor(Model.BlueUser);
   var greenHtml = Html.HtmlHelperFor(Model.GreenUser);
}

@using (blueHtml.BeginForm("BluePost", null)) {
   @blueHtml.EditorForModel()
}

@using (greenHtml.BeginForm("GreenPost", null)) {
   @greenHtml.EditorForModel()
}

最后,在 Controller 中为每个表单创建不同的 POST 操作:

[HttpPost]
public ActionResult BluePost(BlueUser model) {
   ...
}

[HttpPost]
public ActionResult GreenPost(GreenUser model) {
   ...
}

扩展方法如下:

public static class HtmlHelperFactoryExtensions {

   public static HtmlHelper<TModel> HtmlHelperFor<TModel>(this HtmlHelper htmlHelper) {
      return HtmlHelperFor(htmlHelper, default(TModel));
   }

   public static HtmlHelper<TModel> HtmlHelperFor<TModel>(this HtmlHelper htmlHelper, TModel model) {
      return HtmlHelperFor(htmlHelper, model, null);
   }

   public static HtmlHelper<TModel> HtmlHelperFor<TModel>(this HtmlHelper htmlHelper, TModel model, string htmlFieldPrefix) {

      var viewDataContainer = CreateViewDataContainer(htmlHelper.ViewData, model);

      TemplateInfo templateInfo = viewDataContainer.ViewData.TemplateInfo;

      if (!String.IsNullOrEmpty(htmlFieldPrefix))
         templateInfo.HtmlFieldPrefix = templateInfo.GetFullHtmlFieldName(htmlFieldPrefix);

      ViewContext viewContext = htmlHelper.ViewContext;
      ViewContext newViewContext = new ViewContext(viewContext.Controller.ControllerContext, viewContext.View, viewDataContainer.ViewData, viewContext.TempData, viewContext.Writer);

      return new HtmlHelper<TModel>(newViewContext, viewDataContainer, htmlHelper.RouteCollection);
   }

   static IViewDataContainer CreateViewDataContainer(ViewDataDictionary viewData, object model) {

      var newViewData = new ViewDataDictionary(viewData) {
         Model = model
      };

      newViewData.TemplateInfo = new TemplateInfo { 
         HtmlFieldPrefix = newViewData.TemplateInfo.HtmlFieldPrefix 
      };

      return new ViewDataContainer {
         ViewData = newViewData
      };
   }

   class ViewDataContainer : IViewDataContainer {

      public ViewDataDictionary ViewData { get; set; }
   }
}

关于asp.net-mvc - 一个 View 中的两个模型,仅验证其中一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11178593/

相关文章:

ajax - jquery mobile ajax同时发送GET和POST请求

java - 如何以编程方式使 View 在布局上居中?

mysql - 显示 View sql 时出错

asp.net-mvc - System.Web.Mvc中不再存在OnActionExecuting(FilterExecutingContext)吗?

asp.net-mvc - 如何在 ASP.net/ASP.net MVC 中开发基于可插拔/可安装模块的应用程序

c# - 如何有选择地验证一些数据注释属性?

asp.net-mvc - ASP.NET MVC : How do I output the Controller and View that is currently being rendered?

asp.net - 不支持的关键字: 'metadata'。?在带有MVC3的 Entity Framework 中使用Sql连接

asp.net - 为什么“路由”选项卡没有出现在Glimpse Web调试器中?

sql-server - 存储过程,并从两个几乎相同的 View 中进行选择