【问题标题】:MVC3 - Partial Views and server validation of their associated modelMVC3 - 相关模型的部分视图和服务器验证
【发布时间】:2011-12-04 10:03:10
【问题描述】:

我是 MVC3 的新手,对在服务器端进行的模型自动验证有疑问。

我的场景: 具有部分视图(我们称之为部分 A)和关联模型(例如客户名称)的索引页面。在这个局部视图中是另一个局部视图(我们称之为 Partial B),它允许客户输入他们拥有的任何以前的名字(即婚前姓名详细信息),它有自己的模型。

现在 Part B 是可选的,因为用户不必输入详细信息,除非他们愿意,而 Part A 的详细信息必须输入。

按下提交按钮时,包含 Partial 视图的表单会触发控制器和相关操作/方法 - MVC3 会自动验证 Partial A 模型。如果我将两个模型都作为参数传递给动作,那么两者都会被验证。

但是,如果客户添加了详细信息,我想每次都验证 Partial A 模型,并且只验证 Partial B 模型。

所以,我想知道在这种情况下编写代码的最佳方法是什么。
我可以看到表单标记属性可以通过 jquery/javascript 更改,方法是检查是否在 Partial B 上输入了详细信息,然后将操作/方法调用更改为将两个模型作为输入的调用。这是最好的方法,还是有其他更好的方法? 谢谢

【问题讨论】:

    标签: asp.net-mvc-3 validation model


    【解决方案1】:

    如果用户想要输入它们,您可以禁用部分页面 b 控件并启用它们。因为禁用控件没有得到验证。可能使用的另一个选项是使用条件验证。您可以在 google 上对 asp.net mvc 进行条件验证,并获得许多参考链接,您可能会看到 here here

    【讨论】:

      【解决方案2】:

      在我之前的回答中对问题略有误解后,user1079925 在评论中表示,现在将在单个模型中提供数据。因此,我为这种单一模型方法提供了使用自定义数据注释的替代解决方案(之前的答案将被删除):

      此示例假定用户将输入名字、中间名和姓氏。如果输入了中间名或姓中的任何一个,则必须输入名字。

      型号:

      public class IndexModel
      {
          [RequiredIfOtherFieldEntered("MiddleName", "Surname", ErrorMessage="Please enter the forename")]
          public string Forename { get; set; }
      
          public string MiddleName { get; set; }
      
          public string Surname { get; set; }
      }
      

      索引视图:

      @model MvcApplication6.Models.IndexModel
      
      <h2>Index</h2>
      
      @using (Html.BeginForm())
      {
          <p>Forename: @Html.EditorFor(m => m.Forename) @Html.ValidationMessageFor(m => m.Forename)</p>
      
          <p>If you enter the middle name or the surname then the forename will be required.</p>
      
          <p>Middlename: @Html.EditorFor(m => m.MiddleName)</p>
      
          <p>Surname: @Html.EditorFor(m => m.Surname)</p>
      
          <input type="submit" value="submit"/>
      }
      

      HomeController:

      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              return View();
          }
      
          [HttpPost]
          public ActionResult Index(IndexModel indexModel)
          {
              if (ModelState.IsValid)
              {
                  return RedirectToAction("NextPage");
              }
              else
              {
                  return View();
              }
          }
      }
      

      自定义属性:

      public class RequiredIfOtherFieldEnteredAttribute : ValidationAttribute
      {
          private string[] properties;
      
          public RequiredIfOtherFieldEnteredAttribute(params string[] properties)
          {
              if (properties == null && properties.Length < 1)
              {
                  throw new ArgumentNullException("properties");
              }
      
              this.properties = properties;
          }
      
          protected override ValidationResult IsValid(object value, ValidationContext validationContext)
          {
              foreach (string property in properties)
              {
                  //using System.Reflection.PropertyInfo;
                  PropertyInfo propertyInfo = validationContext.ObjectType.GetProperty(property);
      
                  if (propertyInfo == null)
                  {
                      return new ValidationResult(string.Format("Property '{0}' is undefined.", property));
                  }
      
                  var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
      
                  if (propertyValue != null && !string.IsNullOrEmpty(propertyValue.ToString()))
                  {
                      if (value == null || string.IsNullOrEmpty(value.ToString()))
                          return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
                  }
              }
      
              return null;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2012-10-03
        相关资源
        最近更新 更多