[英]How to pass input value from the form which is not part of any model?
在剃刀视图中,我有一个表格:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
等长时间,但我想将以下这些值传递给控制器:
<div class="form-group">
<input class="form-control text-box single-line valid" id="FetchDate" name="FetchDate" type="date">
<input class="form-control text-box single-line valid" id="FetchTime" name="FetchTime" type="time">
</div>
<div class="form-group">
<div class="col-md-offset-0 col-md-10">
<input type="submit" value="Save" class="btn btn-default" style="width: 200px; font-weight: bold;" />
</div>
</div>
</div>
}
Submit(type="submit)
按钮在Person
控制器中调用POST
编辑操作:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,FirstName")] Person person) {
if (ModelState.IsValid) {
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
我们可以看到,唯一的参数是Person person
,但是在表单中我添加了两个与Person没有共同点的字段,但是我还想将它们中的值传递给提交按钮POST Edit
:
<div class="form-group">
<label class="control-label col-md-2" >If you want this client to be fetched to you later, fullfil the data.</label>
<input class="form-control text-box single-line valid" id="FetchDate" name="FetchDate" type="date">
<input class="form-control text-box single-line valid" id="FetchTime" name="FetchTime" type="time">
</div>
问题:如何将值传递到控制器的POST Edit
操作中,这些值不属于我的Razor视图(上图)中的任何模型?
我试过了,时间为null或空字符串(尚未检查):
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,FirstName")] Person person, [Bind(Include="FetchTime")] String time) {
Debug.WriteLine("Time " + time);
if (ModelState.IsValid) {
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
你可以这样做:
public ActionResult Edit([Bind(Include = "Id,FirstName")] Person person, string FetchDate, string FetchTime) {
if (ModelState.IsValid) {
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.