0

我正在使用日期时间选择器。我选择一天并保存数据,但在我的数据库中,它将值保存为1/1/0001 12:00:00 AM. 谁能告诉我这是为什么?

@model MvcApplication4.Models.Reservation
@using JQueryUIHelpers

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<title>jQuery UI Helpers - @ViewBag.Title</title>
<script src="../../Scripts/jquery-ui.unobtrusive-0.4.0.min.js"   type="text/javascript"></script>
<script src="../../Scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />       

<script src="@Url.Content("~/Scripts/jquery-ui-1.8.19.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.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()) {
    @Html.ValidationSummary(true)

    <div class="editor-label">
        @Html.LabelFor(model => model.Room.Room_number, "Room")
    </div>

    <div class="editor-field">
        @Html.DropDownList("Room_ID", string.Empty)
        @Html.ValidationMessageFor(model => model.Room.Room_number)
    </div>

    <fieldset>
        <legend class="ui-state-legend-default ui-corner-top ui-corner-bottom">Detalii Client</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Guest_ID, "First Name")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Guest.FirstName)
            <div class="editor-label">
                @Html.LabelFor(model => model.Guest_ID, "Last Name")
            </div>

        <div class="editor-field">
            @Html.EditorFor(model => model.Guest.LastName)
            <div class="editor-label">
                @Html.LabelFor(model => model.Guest_ID, "Phone number")
            </div>

        <div class="editor-field">
            @Html.EditorFor(model => model.Guest.Phone)
            @Html.ValidationMessageFor(model => model.Guest_ID)
        </div>
    </fieldset>

    <fieldset>
        <legend class="ui-state-legend-default ui-corner-top ui-corner-bottom">Perioada rezervare</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Data_Check_in)
        </div>
        <div class="editor-field">
            @(Html.JQueryUI().Datepicker("anotherDate").MinDate(DateTime.Today).ShowButtonPanel(true)
      .ChangeYear(true).ChangeMonth(true).NumberOfMonths(2))
            @Html.ValidationMessageFor(model => model.Data_Check_in)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Data_Check_out)
        </div>
        <div class="editor-field">
            @(Html.JQueryUI().Datepicker("CheckoutDate").MinDate(DateTime.Today).ShowButtonPanel(true)
      .ChangeYear(true).ChangeMonth(true).NumberOfMonths(2))
            @Html.ValidationMessageFor(model => model.Data_Check_out)
        </div>
    </fieldset>
    <div class="editor-label">
        @Html.LabelFor(model => model.Preference_ID, "Preference")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Preference_ID", string.Empty)
        @Html.ValidationMessageFor(model => model.Preference_ID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model =>model.Price)
    </div>       

    <div class="editor-field">
        @Html.EditorFor(m =>m.Price)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Card_ID, "CreditCard")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Card_ID", String.Empty)
        @Html.ValidationMessageFor(model => model.Card_ID)
    </div>

    <p><input type="submit" value="Create" /></p>
}

这是我的创建操作:

public ActionResult Create()
{
    ViewBag.Card_ID = new SelectList(db.CreditCards, "CardID", "CardType");
    ViewBag.Guest_ID = new SelectList(db.Guests, "Guest_ID", "FirstName");
    ViewBag.Preference_ID = new SelectList(db.Preferences, "Preference_ID", "Preference_Type");
    ViewBag.Room_ID = new SelectList(db.Rooms, "RoomID", "Room_number");
    return View();
} 

//
// POST: /RS/Create

[HttpPost]
public ActionResult Create(Reservation reservation)
{
    if (ModelState.IsValid)
    {
        db.Reservations.Add(reservation);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    ViewBag.Card_ID = new SelectList(db.CreditCards, "CardID", "CardType", reservation.Card_ID);
    ViewBag.Guest_ID = new SelectList(db.Guests, "Guest_ID", "FirstName", reservation.Guest_ID);
    ViewBag.Preference_ID = new SelectList(db.Preferences, "Preference_ID", "Preference_Type", reservation.Preference_ID);
    ViewBag.Room_ID = new SelectList(db.Rooms, "RoomID", "Room_number", reservation.Room_ID);
    return View(reservation);
}
4

2 回答 2

0
               <input id="dateFormate" type="hidden" value='@System.Threading.Thread.CurrentThread.
CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower().Replace("yyyy", "yy")'/>


            <div>
                    <div>
                        End Date
                    </div>
                    <div>
                        @Html.TextAreaFor(model => model.EndDate)
                    </div>
            </div>


            $('#EndDate').datepicker({
                dateFormat: $('#dateFormate').val(),
                showOn: 'button',
                buttonImageOnly: true,
                buttonImage: '/Content/Calendar.png',
                buttonText: 'Click here (date)'               
            });
于 2012-05-04T13:35:09.923 回答
0

尝试使用它,对我来说它更简单。
在头部放这个:

 <link href="../../Content/css/jquery-ui.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/css/jquery.ui.datepicker.css" rel="stylesheet" type="text/css"/>

    <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.ui.datepicker.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.ui.core.min.js" type="text/javascript"></script>
  <script type="text/javascript">
        $(function () {
            $("#Data_Check_out").datepicker({ dateFormat: 'dd-mm-yy' });
            $("#Data_Check_in").datepicker({ dateFormat: 'dd-mm-yy' });
        });
    </script>

在您看来:

 <%: Html.LabelFor(model => model.Data_Check_in) %>
        <br />
            <%: Html.EditorFor(model => model.Data_Check_in)%>
            <%: Html.ValidationMessageFor(model => model.Data_Check_in) %>
        <br />
            <%: Html.LabelFor(model => model.Data_Check_out) %>
        <br />
            <%: Html.EditorFor(model => model.Data_Check_out) %>
            <%: Html.ValidationMessageFor(model => model.Data_Check_out) %>
        <br />
于 2012-05-04T13:45:10.590 回答