EditorFor string但它正在寻找DateTime

时间:2016-01-25 10:47:14

标签: c# model-view-controller editorfor

标题的EditorFor似乎传递了一个DateTime值,而不是一个字符串,但是当我评论表单时,我无法看到为什么会这样做-group有问题,它运行得很好。

类别:

public class EventInputModel
{
    [Required(ErrorMessage ="Event title is required.")]
    [StringLength(200, ErrorMessage ="The {0} must be between {2} and {1{ characters long.", MinimumLength =1)]
    [Display(Name ="Title")]
    public string Title { get; set; }
    [DisplayFormat(DataFormatString = "{0:t}")]
    [DataType(DataType.DateTime)]
    [DateVal(ErrorMessage = "Incorrect DateTime")]
    [Display(Name ="Date and Time *")]
    public DateTime StartDateTime { get; set; }
    public TimeSpan? Duration { get; set; }
    public string Description { get; set; }
    [MaxLength(200)]
    public string Location { get; set; }
    [Display(Name ="Is Public?")]
    public bool IsPublic { get; set; }
}

enter image description here

@{
    ViewBag.Title = "Create New Event";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Create</title>
</head>
<body>
    <h2>Create</h2>
    @using (Html.BeginForm()) 
    {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>EventInputModel</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>

1 个答案:

答案 0 :(得分:0)

解决: 错字:必须介于{2}和{1 {之间。可能是相关的,可能不是。 - markpsmith 22分钟前