在我的ASP.Net MVC (FX)應用程式中,我正在以這種方式保存兩個日期值:
在我的ASP.Net MVC (FX)應用程式中,我正在以這種方式保存兩個日期值
<div class="form-group"/span>>
@Html.LabelFor(model => model.Hire_Date, "Hire Date"/span>, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @class = "dateepicker" }。})
@Html.ValidationMessageFor(model => model.Hire_Date, "", new { @class = "text-danger" })
</div>
<div style="margin-right:1%"><p style="color:#FF0000"> *</p> </div>
</div>
...
<div class="form-group"/span>>
@Html.LabelFor(model => model.Injury_Date, "Injury Date"/span>, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Injury_Date, new { htmlAttributes = new { @class = "datepicker" } 。})
@Html.ValidationMessageFor(model => model.Injury_Date, "", new { @class = "text-danger" })
</div>
<div style="margin-right:1%"><p style="color:#FF0000"> *</p> </div>
</div>
該欄位的模型看起來是這樣的:
[]
public System.DateTime Hire_Date { get; set; }
[]
public System.DateTime Injury_Date { get; set; }
這樣就能正確地保存到資料庫中了。這個資料也可以被檢索并顯示在以后的螢屏上,比如像這樣的Details.cshtml螢屏:
<dt>
雇用日期
</dt>
<dd>
@Html.DisplayFor(model => model.Hire_Date)
</dd>
......
<dt>
受傷日期
</dt>
<dd>
@Html.DisplayFor(model => model.Injury_Date)
</dd>
這很簡單,也很有意義,但是在Edit.cshtml頁面和其他一些需要編輯這些日期欄位的頁面上,我無法想出如何向用戶顯示當前的日期值,并讓他們有辦法改變它。
這是我目前在Edit.cshtml中的內容:
<div class="form-group"/span>>
@Html.LabelFor(model => model.Hire_Date, "Hire Date"/span>, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @class = "dateepicker" }。})
@Html.ValidationMessageFor(model => model.Hire_Date, "", new { @class = "text-danger" })
</div>
<div style="margin-right:1%"><p style="color:#FF0000"> *</p> </div>
</div>
但是,正如你所看到的,這并沒有向用戶顯示這些日期目前是什么:
盡管他們可以改變這個值。
我怎樣才能既向用戶顯示現有的日期,又允許他們編輯它呢?
編輯1:
我已經更新了Edit.cshtml視圖,使其看起來像這樣:
我已經更新了Edit.cshtml視圖。
<div class="form-group"/span>>
@Html.LabelFor(model => model.Hire_Date, "Hire Date"/span>, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @Value = @Model. Hire_Date.ToString("mm-dd-yyyy"), @class = "datepicker" }。})
@Html.ValidationMessageFor(model => model.Hire_Date, "", new { @class = "text-danger" })
</div>
<div style="margin-right:1%"><p style="color:#FF0000"> *</p> </div>
</div>
在兩個日期上都添加了@Value = @Model.Hire_Date.ToString("mm-dd-yyyy"),但這并沒有改變什么。
編輯2:我包括了我的編輯控制器:
// GET: WC_Inbox/Edit/5
public ActionResult Edit(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest)。
}
WC_Inbox wC_Inbox = db.WC_Inbox.Find(id)。
if (wC_Inbox == null)
{
return HttpNotFound()。
}
Employee employee = db.Employees.Find(wC_Inbox.EmployeesID);
string fullName = employee.First_Name " "/span> employee.Last_Name。
string addUser = wC_Inbox.Add_User;
ViewBag.EmployeeID = id;
ViewBag.Name = fullName;
ViewBag.Add_User = addUser;
return View(wC_Inbox)。
}
// POST: WC_Inbox/Edit/5
[]
[]
public ActionResult Edit([Bind(Include = "ID。 InboxID,EmployeeID,地區,Org_Number,Hire_Date,Job_Title,Work_Schedule,Injury_Date,Injury_Time,DOT_12,Start_Time。 Injured_Body_Part,Side,Missing_Work,Return_Work_Date,Doctors_Release,Treatment,Injury_Description,Equipment,Witness,Questioned,Medical_History,Inbox_Submitted,Inbox_Reason,Comments,User_Email,Contact_Email,Specialist_Email,Option_Email,Option_Email2,Option_Email3,Option_Email4, Add_User"/span>>)] WC_Inbox wC_Inbox))
{
if (ModelState.IsValid)
{
db.Entry(wC_Inbox).State = EntityState.Modified。
try (ModelState.IsValid)
{
db.SaveChanges()。
}
catch (DbEntityValidationException ex)
{
foreach (var errors in ex.EntityValidationErrors)
{
foreach (var validationError in errors.ValidationErrors)
{
//獲得錯誤資訊。
string errorMessage = validationError.ErrorMessage。
System.Diagnostics.Debug.WriteLine(errorMessage)。
}
}
}
return RedirectToAction("Index") 。
}
ViewBag.EmployeeID = new SelectList(db.Employees, "ID", "First_Name", wC_Inbox.EmployeeID) 。
return View(wC_Inbox)。
編輯3:
這就是那些日期值最初在Create.cshtml頁面中的輸入方式:
<div class="form-group"/span>>
@Html.LabelFor(model => model.Hire_Date, "Hire Date"/span>, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @class = "dateepicker" }。})
@Html.ValidationMessageFor(model => model.Hire_Date, "", new { @class = "text-danger" })
</div>
<div style="margin-right:1%"><p style="color:#FF0000"> *</p> </div>
</div>
uj5u.com熱心網友回復:
當你用[DataType(DataType.Date)]設定屬性時,你需要按照下面的方法來設定值
。@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @Value = @Model.Hire_Date.ToString("mm-dd-yyyy"), @class = "datepicker" })。})
uj5u.com熱心網友回復:
由于編輯器正在渲染HTML日期輸入,value屬性需要使用yyyy-MM-dd格式,以便瀏覽器將其識別為有效日期。然后,瀏覽器將根據用戶的語言設定,以他們期望的格式向用戶顯示該值。
你可以修改EditorFor的呼叫來指定日期格式:
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { value = Model. Hire_Date.ToString("yyyy-MM-dd"), @class = "datepicker" }。})
或者你可以在模型上指定格式字串,并從EditorFor呼叫中洗掉value屬性:
[DataType(DataType.Date)/span>]
[DisplayFormat(DataFormatString = "{0:yyy-MM-dd}", ApplyFormatInEditMode = true)/span>]
public System.DateTime Hire_Date { get; set; }
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @class = "datepicker" }. })
或者你可以在`~/Views/Shared/EditorTemplates/Date.cshtml'中添加一個自定義的編輯器模板:
@model DateTime?
@{
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]) 。
htmlAttrributes["type"] = "date"。
}
@Html.TextBox("", Model?.ToString("yyy-MM-dd"), htmlAttributes)
[DataType(DataType.Date)]
public System.DateTime Hire_Date { get; set; }
@Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @class = "datepicker" }. })
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310880.html
標籤:
上一篇:在R中組織資料的行數

