在MVC中获得越狱异常

时间:2017-09-14 15:38:23

标签: asp.net-mvc validation data-annotations

我跟着这个(https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/getting-started-with-mvc/getting-started-with-mvc-part7),但是当我查看网页时,我看到一些奇怪的事情。

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>

1 个答案:

答案 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; }