2017-09-14 70 views
1

我正在關注這個(https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/getting-started-with-mvc/getting-started-with-mvc-part7),但我在查看網頁時看到了一些奇怪的事情。在MVC中獲取vailidation異常

1)ReleaseDate說它是一個必填字段(即使它沒有在代碼中標記),我不明白爲什麼它這樣做。

2)Price 「作品」,如果值是100.50,或更小。如果它的100.51或更高,那麼這個消息就會啓動。我的理解是這個消息應該踢在@100.01 ......或者我錯了嗎?

namespace Movies.Models 
{ 
using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 

public partial class Movie 
{ 
    public int Id { get; set; } 

    [Required(ErrorMessage = "Titles are required")] 
    public string Title { get; set; } 

    public System.DateTime ReleaseDate { get; set; } 

    public string Genre { get; set; } 

    [Required(ErrorMessage = "The Price is required.")] 
    [Range(5, 100, ErrorMessage = "Movies cost between £5 and £100.")] 
    public decimal Price { get; set; } 
} 
} 

enter image description here

有人能指出我在做什麼錯?

感謝

視圖代碼是

@model Movies.Models.Movie 

@{ 
ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Movie</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.ReleaseDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.ReleaseDate, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Genre, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Genre, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 
+0

哪裏是你的視圖代碼? –

回答

1

第一個問題。

讓您DateTime可空這樣的:

public System.DateTime? ReleaseDate { get; set; } 

第二個問題:

指定範圍數類型​​double與字面d這樣的:

[Range(5d, 100d, ErrorMessage = "Movies cost between £5 and £100.")] 
public decimal Price { get; set; } 
+0

我修改了它以[範圍(5.00,100.00,ErrorMessage =「電影成本介於5英鎊和100英鎊之間。」)] –

+0

只需要弄清楚ReleaseDate是在說什麼它的要求,即使我沒有說過它是。 –

+0

@GeraldOakham基本上是一樣的。 Yout dots'''使你的numers'double'。字面意思是'M'使數字成爲'decimal'。所以你的解決方案一般可能仍然是錯誤的。 –