如何使选择的日期以某种格式保存?
正如主题所述,我希望在使用 ASP.NET MVC 框架开发的 Web 应用程序中以 dd-mm-yyyy 格式选择的日期是保存到 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>
}
As the subject stated, I want to make the date selected in dd-mm-yyyy
format in my web application develop using asp.net mvc framework is the date saved to the table in mysql database.
Here is the 'scenario':
In the date filling field, I make a format dd-mm-yyyy
, and use datetimepicker to help user select the date, but it seems mySQL use mm-dd-yyyy when save that data, so when I choose 12 February 2012
(in the filling field it shows 12-02-2012
), it will be saved as 2 December 2012
.
What should I do so make mySQL understand that I want to save 12 February 2012 and not 2 December 2012?
Here is what I have in the controller method:
The GET method
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);
}
The HttpPost method
[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");
}
}
The view:
@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>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想知道将其添加到 my.ini 并重新启动 mysql 是否会改变任何内容:
I wonder if adding this to my.ini and restarting mysql changes anything:
您可以更改当前日期格式,如下所示:
Now.ToString("dd-MM-yyyy")
祝卫星好运,
Cerveser
You could change the now date format as follows:
Now.ToString("dd-MM-yyyy")
Good luck with the Satelites,
Cerveser