2015-11-14 43 views
-1

我需要在我的asp.net mvc 4項目中使用Range Validator。在視圖模型我說:MVC 4範圍驗證器不接受最小值

[Range(0, int.MaxValue, ErrorMessage = "The Region field is required.")] 
public int Region { get; set; } 

並在控制器我通過ModelState.IsValid檢查它。

問題是ModelState中不接受0作爲正常價值 - 這是奇怪的,因爲我從0max int設置範圍,所以我想0屬於集合(像MVC 5,如果我記得)。我錯了還是隻是一個錯誤?任何其他值正在正確讀取(-1不接受並且1001沒問題)。

更新1

Debug 1 enter image description here

(就像你看到的上面的照片我不和我的模型東西之前我沒有檢查,如果模型是有效的)。

註冊方法:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public ActionResult Register(RegisterModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     //I was never here! Always ModelState.IsValid returns false 
    } 

    // If we got this far, something failed, redisplay form 
    ViewBag.Regions = _database.Department.ToList(); 
    return View(model); 
} 

全RegisterViewModel類:

public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "User name")] 
    public string UserName { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    [Required] 
    [Display(Name = "Role")] 
    public int Role { get; set; } 

    [Range(0, int.MaxValue, ErrorMessage = "The Region field is required.")] 
    [Display(Name = "Region")] 
    public int Region { get; set; } 
} 

註冊視圖:

<fieldset> 
     <legend>Registration Form</legend> 
     <ol> 
      <li> 
       @Html.LabelFor(m => m.UserName) 
       @Html.TextBoxFor(m => m.UserName) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.Password) 
       @Html.PasswordFor(m => m.Password) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.ConfirmPassword) 
       @Html.PasswordFor(m => m.ConfirmPassword) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.Role) 
       <select id="Role" name="Role" data-val-required="The Role field is required." data-val="true"> 
        <option value="">Choose role for user</option> 
        <option value="1">Administrator</option> 
        <option value="2">Inspector</option> 
       </select> 
      </li> 
      <li id="region-section"> 
       @Html.LabelFor(m => m.Region) 
       <select id="Region" name="Region" data-val-required="The Region field is required." data-val="true"> 
        <option value="" selected>Choose region</option> 
        @foreach (var item in ViewBag.Regions) 
        { 
         <option value="@item.Id">@item.Name</option> 
        } 
       </select> 
      </li> 
     </ol> 
     <input type="submit" value="Register" /> 
    </fieldset> 

UPDATE 2

根據@pilotcam的建議,我用ValidateModel()函數。在調試模式下,我得到了HResult:-2146233079 - 我試圖將它轉換爲系統錯誤代碼(),但不是System Error Codes list的一部分。

+0

這不應該發生。你調試過,沒有看到其他問題? –

+0

@ArghyaC:是的,我是調試和ModelState.IsValid只有在區域鍵false。我的RegisterViewModel的Rest鍵是真的。 –

+1

在'RangeAttribute'中,包含最小值和最大值。我剛剛檢查了源代碼。他們很好。看看別的東西是否正在改變這些值,或者是否還有其他的錯誤。 –

回答

1

Finnaly我找到了答案。問題出在我的領域(public int Region { get; set; })。如果我沒有設置任何東西,mvc嘗試將null設置爲Region,但是int在控制器調試過程中不能爲空,所以在控制器中我仍然得到0而不是null這是不明確的。我沒有[Required]屬性,但不是可以爲空的類型自動添加它(錯誤名稱「X字段是必需的」...是的,就像我的自定義錯誤消息在[Range]屬性)。

解決的辦法是改變:

public int Region { get; set; } 

到:

public int? Region { get; set; } 

...在視圖模型。

+1

謝謝,我知道你在你最初的問題上有負面的觀點,但是當模型沒有被填充時,null驗證爲false並不明顯,除了回想起來。 –

1

而不是使用ViewBag,我建議增加一個區域屬性您註冊類,如下所示:

public class RegisterModel 
{ 
    // ... existing code 

    public SelectList Regions { get; set; } 
} 

這需要一些更新到你的動作方法:

// your GET action 
public ActionResult Register() 
{ 
    var model = new RegisterModel(); 
    model.Regions = new SelectList(_database.Department, "Id", "Name", model.Region); 

    return View(model); 
} 

然後,最後,更新你的視圖:

<li id="region-section"> 
    @Html.LabelFor(m => m.Region) 
    @Html.DropDownListFor(m => m.Region, Model.Regions, "Select one...") 
</li> 

這似乎比使用View更一致根據我的經驗,手工編寫並編寫<select>元素。

如果仍然都可以從驗證奇怪的結果,我建議你安裝Glimpse,並檢查出model binding tab,它會顯示您的公佈值是如何被映射。