2017-09-14 43 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'。所以你的解决方案一般可能仍然是错误的。 –