0

如果您没有选择至少一个,则模型验证不会评估链接到列表框值的属性。这种方式不可能使用 DataAnnotations 进行模型评估以告知所需的值。

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestValidation.Models;

namespace TestValidation.Controllers
{
  [HandleError]
  public class HomeController : Controller
  {
    private SelectList list = new SelectList(new List<string>()
        {
            "Sao Paulo",
            "Toronto",
            "New York",
            "Vancouver"
        });
    public ActionResult Index()
    {
      ViewData["ModelState"] = "NOT EVAL";
      ViewData["ItemsList"] = list;
      return View();
    }
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(MyEntity entity)
    {
      if (ModelState.IsValid)
      {
        ViewData["ModelState"] = "VALID";
      }
      else
      {
        ViewData["ModelState"] = "NOT VALID!!!";
      }
      ViewData["ItemsList"] = list;
      return View();
    }

    public ActionResult About()
    {
      return View();
    }

  }
}

风景:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestValidation.Models.MyEntity>" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
  Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
  <h2>
    Validation Test</h2>
  <p>
  <% using (Html.BeginForm())
     {%>
  <fieldset>
    <p>
      ModelState:
      <%= Html.Encode((string)ViewData["ModelState"])%>
    </p>
    <p>
      <label for="Name">
        Name:</label>
      <%= Html.TextBoxFor(m => m.Name)%>
      <%= Html.ValidationMessageFor(m => m.Name)%>
    </p>
    <p>
      <label for="ItemFromList">
        Items (list):</label>
      <%= Html.ListBoxFor(m => m.ItemFromList, ViewData["ItemsList"] as SelectList)%>
      <%= Html.ValidationMessageFor(m => m.ItemFromList)%>
    </p>
    <p>
      <label for="ItemFromCombo">
        Items (combo):</label>
      <%= Html.DropDownListFor(m => m.ItemFromCombo, ViewData["ItemsList"] as SelectList)%>
      <%= Html.ValidationMessageFor(m => m.ItemFromCombo)%>
    </p>
    <p>
      <input type="submit" value="Submit" />
    </p>
  </fieldset>
  <% } %>
</asp:Content>

该模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;


namespace TestValidation.Models
{
  public class MyEntity_Validate : ValidationAttribute
  {
    public MyEntity_Validate()
    {
      this.ErrorMessage = "Validated!. Is <> Toronto";
    }
    public override bool IsValid(object value)
    {
      return ((string)value == "Toronto");
    } 
  }
  public class MyEntity
  {
    [Required]
    public string Name { get; set; }
    [MyEntity_Validate]
    public string ItemFromList { get; set; }
    [MyEntity_Validate]
    public string ItemFromCombo { get; set; }
  }
}

任何帮助将不胜感激。谢谢你。

4

2 回答 2

0

该问题是已知的,但 MVC 开发团队目前没有更改功能的计划 ( http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=4858 )。

spmason 博客文章:http ://spmason.com/post/343293206/why-aspnetmvc-2-is-broken

就个人而言,我同意你的观点,即功能令人困惑。如果某个字段是必填的,但没有传入,则 ModelState 无效。

于 2010-01-20T15:39:05.133 回答
0

真是个好消息!

在 VS2010 RC 中需要更新 ASP.NET MVC 的版本。请检查:http ://www.nitrix-reloaded.com/2010/03/15/install-asp-net-mvc-2-rtm-on-vs2010-rc/

于 2010-03-31T12:39:29.587 回答