asp.net MVC Web 应用程序日期选取器不起作用



这是我的 Create.cshtml 文件

@model Bartering.Models.Advertisement

@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">
</script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">
</script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" 
type="text/javascript"></script>
@using (Html.BeginForm("Create", "Advertisement", FormMethod.Post,  new { enctype="multipart/form-data" })) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>TASK</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Task_For)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Task_For)
            @Html.ValidationMessageFor(model => model.Task_For)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Department)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Department,(SelectList)ViewBag.CategoryList)
            @Html.ValidationMessageFor(model => model.Department)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Attachment)
        </div>
        <div class="editor-field">
            <input type="file" id="MyFile" runat="server" />
            @Html.ValidationMessageFor(model => model.Attachment)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Date)
        </div>
       @inherits System.Web.Mvc.WebViewPage<System.DateTime?> 
       @Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : DateTime.Today.ToShortDateString()), new { @class = "datefield" })      
        <div class="editor-field">
            @Html.EditorFor(model => model.Date)
            @Html.ValidationMessageFor(model => model.Date)
        </div>

        <p>
            <input type="submit" value="Post"  />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这是我的广告模型类

 using System;
 using System.Drawing; // Image type is in this namespace
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
 using System.ComponentModel;
 namespace Bartering.Models
 {
        public class Advertisement
        {
            [Key]
            public int ID { get; set; }
            [Required]
            [StringLength(100)]
            public string Task_For { get; set; }
            public Guid OwnerID { get; set; }
            public string UserName { get; set; }
            [Required]
            public string Department { get; set; }

            public byte[] Attachment { get; set; }
            [Required]
            [StringLength(200)]
            public string Description { get; set; }
            public int Count { get; set; }
            [DisplayName("Date")]
            [DataType(DataType.DateTime,ErrorMessage="Date non valid")]
            [DisplayFormat(DataFormatString="{0:MM/dd/yy}",ApplyFormatInEditMode=true)]
            [Required]
            public DateTime Date { get; set; }

        }
 }

当我运行我的应用程序时,它给出以下错误

The 'inherits' keyword is not allowed when a 'model' keyword is used.
Line 66:        @inherits System.Web.Mvc.WebViewPage<System.DateTime?>

我想添加一个日期选择器来添加日期。我该如何解决这个问题,请帮我解决这个问题。

在您看来,您有

  @inherits System.Web.Mvc.WebViewPage<System.DateTime?> 
       @Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") :

只需删除继承行"@inherits系统.Web.Mvc.WebViewPage"

评论后更新

请阅读有关继承的信息 http://odetocode.com/blogs/scott/archive/2011/01/13/razor-tip-2-inheritance-amp-configuration.aspx

简单地说继承让知道剃刀的时间将是你的@Model财产。要使日期选择器工作,您必须@Html.EditorFor(model => model。日期),它将呈现(Html5 日期选择器)它将在现代浏览器中工作。

为了使您的第二个示例工作,请遵循格式

@Html.TextBox("name", "value", new {type="date"})

最新更新