ASP.NET MVC – 使用Html.BeginForm传递模型数据和非模型隐藏字段值
ASP.NET MVC – Use Html.BeginForm to pass model data and non-model hidden field value
我正在使用 BeginForm 传递模型数据(即 last_coffe_time),效果很好。但是,我还想将存储在不属于模型的隐藏字段中的控制器 ClientDateTime 值传递给控制器。
我不知道该怎么做,充其量我可以将 ClientDateTime 作为静态字符串传递,但我不知道如何动态读取隐藏字段的值并将该值传递给控制器。
有人请帮助。谢谢
查看:
@model SleepNotes.Models.Diary
@using (Html.BeginForm("Edit", "Diary", new { ClientDateTime = "ClientDateTime" }, FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.Hidden("ClientDateTime", new { id = "ClientDateTime" })
<div class="form-group">
@Html.LabelFor(model => model.last_coffe_time, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.last_coffe_time, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.last_coffe_time, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<!--Set ClientDateTime-->
<script>
var a = new Date()
var month = a.getMonth() + 1;
document.getElementById('ClientDateTime').value = a.getDate() + "/" + month + "/" + a.getFullYear() + " " + a.getHours() + ":" + a.getMinutes() + ":" + a.getSeconds();
</script>
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "id,last_coffe_time")] Diary Diary, string ClientDateTime)
{
if (ModelState.IsValid)
{
//ClientDateTime from view ...do something here
db.Entry(Diary).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index", "Dashboard");
}
return View(Diary);
}
有一些解决方案,
- 我建议您在模型和视图之间有一个模型视图。那
模态视图应该有 ClientDateTime。
- 您可以使用 ajax post 并向控制器或您发送 2 个参数(yourmodal,字符串)
可以将 ClientDateTime 作为查询字符串传递。有很多
关于它的例子。
希望有所帮助。
从 BeginForm()
方法中删除 new { ClientDateTime = "ClientDateTime" }
,这样就不会将值添加为路由参数(因此不会绑定到 Edit()
中的 string ClientDateTime
参数方法。
旁注:由于您想要提交 DateTime
值,因此您的参数应该是 DateTime ClientDateTime
(而不是 string
)并且您的脚本可以简化为
document.getElementById('ClientDateTime').value = new Date().toISOString();
我正在使用 BeginForm 传递模型数据(即 last_coffe_time),效果很好。但是,我还想将存储在不属于模型的隐藏字段中的控制器 ClientDateTime 值传递给控制器。 我不知道该怎么做,充其量我可以将 ClientDateTime 作为静态字符串传递,但我不知道如何动态读取隐藏字段的值并将该值传递给控制器。
有人请帮助。谢谢
查看:
@model SleepNotes.Models.Diary
@using (Html.BeginForm("Edit", "Diary", new { ClientDateTime = "ClientDateTime" }, FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.Hidden("ClientDateTime", new { id = "ClientDateTime" })
<div class="form-group">
@Html.LabelFor(model => model.last_coffe_time, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.last_coffe_time, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.last_coffe_time, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<!--Set ClientDateTime-->
<script>
var a = new Date()
var month = a.getMonth() + 1;
document.getElementById('ClientDateTime').value = a.getDate() + "/" + month + "/" + a.getFullYear() + " " + a.getHours() + ":" + a.getMinutes() + ":" + a.getSeconds();
</script>
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "id,last_coffe_time")] Diary Diary, string ClientDateTime)
{
if (ModelState.IsValid)
{
//ClientDateTime from view ...do something here
db.Entry(Diary).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index", "Dashboard");
}
return View(Diary);
}
有一些解决方案,
- 我建议您在模型和视图之间有一个模型视图。那 模态视图应该有 ClientDateTime。
- 您可以使用 ajax post 并向控制器或您发送 2 个参数(yourmodal,字符串) 可以将 ClientDateTime 作为查询字符串传递。有很多 关于它的例子。
希望有所帮助。
从 BeginForm()
方法中删除 new { ClientDateTime = "ClientDateTime" }
,这样就不会将值添加为路由参数(因此不会绑定到 Edit()
中的 string ClientDateTime
参数方法。
旁注:由于您想要提交 DateTime
值,因此您的参数应该是 DateTime ClientDateTime
(而不是 string
)并且您的脚本可以简化为
document.getElementById('ClientDateTime').value = new Date().toISOString();