在剃刀视图中,我有一个表单:
@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)
按钮在POST
控制器中调用Person
编辑操作:
[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视图中的任何模型(上图)?
我试过这个,时间为空或空字符串(还没有检查):
[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);
}
答案 0 :(得分:1)
你可以这样做:
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);
}