正如主题所述,我想在我的Web应用程序中使用dd-mm-yyyy
格式选择日期使用asp.net mvc框架开发是将日期保存到mysql数据库中的表。
这是“情景”:
在日期填充字段中,我创建了一个格式dd-mm-yyyy
,并使用datetimepicker来帮助用户选择日期,但是在保存该数据时似乎mySQL使用mm-dd-yyyy,所以当我选择12 February 2012
时(在填充字段中显示12-02-2012
),它将保存为2 December 2012
。
我应该怎么做才能让mySQL理解我想要保存2012年2月12日而不是2012年12月2日?
以下是我在控制器方法中的内容:
GET方法
public ActionResult AddAssignment()
{
var satqry = db.Satelites.Select(c => new
{
c.SateliteID,
c.SateliteName
});
var wmqry = db.JustUsers
.Select(u => new
{
u.name,
u.RealName
})
.OrderBy( u => u.RealName);
SateliteSchedule satsched = new SateliteSchedule();
satsched.Tanggal = DateTime.Now.Date;
ViewBag.SatList = new SelectList(satqry.AsEnumerable(), "SateliteID", "SateliteName");
ViewBag.WMList = new SelectList(wmqry.AsEnumerable(), "name", "RealName");
return View(satsched);
}
HttpPost方法
[HttpPost]
public ActionResult AddAssignment(SateliteSchedule SatSched)
{
var txt = "";
if (ModelState.IsValid)
{
if (SatSched.ID == 0)
{
db.SateliteSchedules.Add(SatSched);
txt = "{0} has been added!";
}
else
{
db.Entry(SatSched).State = EntityState.Modified;
txt = "{0} has been modified!";
}
db.SaveChanges();
Utility utl = new Utility();
TempData["message"] = string.Format(txt, utl.GetSateliteName(SatSched.SateliteID));
return RedirectToAction("FormAssignment");
}
else
{
ViewBag.Message = "ModelState is not Valid!";
return View("ErrorView");
}
}
观点:
@using (Html.BeginForm("AddAssignment", "admin", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(m => m.ID);
<table>
<tr>
<td>@Html.LabelFor(m => m.Tanggal)
</td>
<td>
@Html.EditorFor(m => m.Tanggal)
@Html.ValidationMessageFor(m => m.Tanggal)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.SateliteID)</td>
<td>
@Html.DropDownList("SateliteID", (IEnumerable<SelectListItem>)ViewBag.SatList, "--- Satelite ---")
@Html.ValidationMessageFor(m => m.SateliteID)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.WMOnDuty)</td>
<td>
@Html.DropDownList("WMOnDuty", (IEnumerable<SelectListItem>)ViewBag.WMList, "--- Worship Manager ---")
@Html.ValidationMessageFor(m => m.WMOnDuty)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.SMOnDuty)</td>
<td>@Html.EditorFor(m => m.SMOnDuty)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.WLOnDuty)</td>
<td>@Html.EditorFor(m => m.WLOnDuty)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.MLOnDuty)</td>
<td>@Html.EditorFor(m => m.MLOnDuty)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.SoundMan)</td>
<td>@Html.EditorFor(m => m.SoundMan)</td>
</tr>
<tr>
<td valign=top>@Html.LabelFor(m => m.Note)</td>
<td>@Html.TextAreaFor(model => model.Note, new { @class = "memo-text" })</td>
</tr>
</table>
<div>
<input type="submit" value="Save" />
@Html.ActionLink("Kembali", "FormAssignment")
</div>
}
答案 0 :(得分:0)
我想知道是否将此添加到my.ini并重新启动mysql会改变任何内容:
[mysqld]
date_format="%Y%m%d"
datetime_format="%d-%m-%Y %H:%i:%s"
答案 1 :(得分:0)
您可以更改现在的日期格式,如下所示: Now.ToString(&#34; DD-MM-YYYY&#34)
祝卫卫星好运,
Cerveser