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

时间:2014-01-02 05:49:21

标签: c# jquery asp.net asp.net-mvc database

这是我的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?>

我想添加一个日期选择器来添加date.how我会解决这个问题,请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

在你看来,你有

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

只需删除继承行“@inherits System.Web.Mvc.WebViewPage”

评论后更新

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

的内容

简单地说说继承让我知道剃刀的时间是你的@Model属性。 要使日期选择器工作,你有@ Html.EditorFor(model =&gt; model.Date),它将呈现(Html5日期选择器),它将在现代浏览器中工作。

为了让您第二个工作示例,请按照格式

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