模型数据在插入 MVC 后未绑定到文本框
Model Data not binding to textboxfor after insertion in MVC
我在 mvc 4 中遇到问题,我创建了操作方法,我将数据插入数据库,然后 return 模型要查看,但模型数据未绑定到隐藏字段,如 Id 和 Voucher数字,
在其他字段中,数据可以正确绑定,但问题在于这些 Id 和 VoucherNum,Id 是主键,而 VoucherNum 是唯一的。
我已经提到了代码和 html.
代码:
[HttpPost]
public ActionResult Create(Payment payment)
{
payment.VoucherNum = db.Payments.Count() + 1;
Ledger ledger = new Ledger();
ledger.CusId = payment.CustomerId;
ledger.Date = payment.Date;
ledger.Remarks = payment.Remarks;
ledger.Type = payment.Type;
string negativeAmount;
negativeAmount = "-" + payment.Amount;
ledger.Amount = Convert.ToInt32(negativeAmount);
ledger.IsActive = true;
payment.IsActive = true;
db.Payments.Add(payment);
db.Ledgers.Add(ledger);
db.SaveChanges();
ViewBag.CustomerId = new SelectList(db.Customers.ToList()
.Select(x => new { Id = x.Id, CustomerCode = x.Name + "-" + x.CustomerCode }
), "Id", "CustomerCode", payment.CustomerId);
var model = db.Payments.Find(payment.Id);
return View(model);
}
<h2>Payments</h2>
<hr />
<div class="row">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Vocher#</label>
@Html.TextBoxFor(model => model.VoucherNum, new { @class = "form-control", @readonly="true"})
@Html.HiddenFor(model=>model.Id)
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Customer</label>
@Html.DropDownList("CustomerId", (IEnumerable<SelectListItem>)ViewBag.CusId, "--Select Customer--", new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Date</label>
@Html.TextBoxFor(model => model.Date, new { @class = "form-control", @id = "date"})
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Amount</label>
@Html.TextBoxFor(model => model.Amount, new { @class = "form-control" })
</div>
<div class="form-group">
<label class="control-label">Type</label>
@Html.TextBoxFor(model => model.Type, new { @class = "form-control" })
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<label class="control-label">Remarks</label>
@Html.TextAreaFor(model => model.Remarks, new { @class = "form-control", @rows = "5" })
</div>
<input type="submit" value="Payment Receive" class="btn btn-primary pull-right" />
</div>
}
</div>
因为辅助方法将首先查看模型状态字典以填充输入的值。在您保存它之前,模型状态字典当前具有 VoucherNum
的空值,辅助方法将使用此值生成输入字段的值。
要解决此问题,您可以在 return 进入视图之前明确清除模型状态字典,
db.Payments.Add(payment);
db.SaveChanges();
ModelState.Clear();
var p = db.Payments.Find(model.Id);
return View(p);
或更好,遵循PRG模式。使用 Post-Redirect-Get 模式,成功更新数据库后,您将 return 重定向响应返回给客户端,客户端将发出一个完全对服务器的新 GET 请求。
对于您的情况,您可以使用 RedirectToAction
来 return 302 响应。
db.SaveChanges();
return RedirectToAction("Edit",new {id=payment.Id});
这将告诉浏览器使用请求中的新 ID url 为 Edit
操作方法发出新的 GET 请求。您的 GET 操作方法将使用此 ID 并从 db 和 return 中获取实体。
public ActionResult Edit(int id)
{
var model = db.Payments.Find(id);
return View(model);
}
我强烈建议您使用 PRG 模式。
我在 mvc 4 中遇到问题,我创建了操作方法,我将数据插入数据库,然后 return 模型要查看,但模型数据未绑定到隐藏字段,如 Id 和 Voucher数字, 在其他字段中,数据可以正确绑定,但问题在于这些 Id 和 VoucherNum,Id 是主键,而 VoucherNum 是唯一的。 我已经提到了代码和 html.
代码:
[HttpPost]
public ActionResult Create(Payment payment)
{
payment.VoucherNum = db.Payments.Count() + 1;
Ledger ledger = new Ledger();
ledger.CusId = payment.CustomerId;
ledger.Date = payment.Date;
ledger.Remarks = payment.Remarks;
ledger.Type = payment.Type;
string negativeAmount;
negativeAmount = "-" + payment.Amount;
ledger.Amount = Convert.ToInt32(negativeAmount);
ledger.IsActive = true;
payment.IsActive = true;
db.Payments.Add(payment);
db.Ledgers.Add(ledger);
db.SaveChanges();
ViewBag.CustomerId = new SelectList(db.Customers.ToList()
.Select(x => new { Id = x.Id, CustomerCode = x.Name + "-" + x.CustomerCode }
), "Id", "CustomerCode", payment.CustomerId);
var model = db.Payments.Find(payment.Id);
return View(model);
}
<h2>Payments</h2>
<hr />
<div class="row">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Vocher#</label>
@Html.TextBoxFor(model => model.VoucherNum, new { @class = "form-control", @readonly="true"})
@Html.HiddenFor(model=>model.Id)
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Customer</label>
@Html.DropDownList("CustomerId", (IEnumerable<SelectListItem>)ViewBag.CusId, "--Select Customer--", new { @class = "form-control" })
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Date</label>
@Html.TextBoxFor(model => model.Date, new { @class = "form-control", @id = "date"})
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Amount</label>
@Html.TextBoxFor(model => model.Amount, new { @class = "form-control" })
</div>
<div class="form-group">
<label class="control-label">Type</label>
@Html.TextBoxFor(model => model.Type, new { @class = "form-control" })
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<label class="control-label">Remarks</label>
@Html.TextAreaFor(model => model.Remarks, new { @class = "form-control", @rows = "5" })
</div>
<input type="submit" value="Payment Receive" class="btn btn-primary pull-right" />
</div>
}
</div>
因为辅助方法将首先查看模型状态字典以填充输入的值。在您保存它之前,模型状态字典当前具有 VoucherNum
的空值,辅助方法将使用此值生成输入字段的值。
要解决此问题,您可以在 return 进入视图之前明确清除模型状态字典,
db.Payments.Add(payment);
db.SaveChanges();
ModelState.Clear();
var p = db.Payments.Find(model.Id);
return View(p);
或更好,遵循PRG模式。使用 Post-Redirect-Get 模式,成功更新数据库后,您将 return 重定向响应返回给客户端,客户端将发出一个完全对服务器的新 GET 请求。
对于您的情况,您可以使用 RedirectToAction
来 return 302 响应。
db.SaveChanges();
return RedirectToAction("Edit",new {id=payment.Id});
这将告诉浏览器使用请求中的新 ID url 为 Edit
操作方法发出新的 GET 请求。您的 GET 操作方法将使用此 ID 并从 db 和 return 中获取实体。
public ActionResult Edit(int id)
{
var model = db.Payments.Find(id);
return View(model);
}
我强烈建议您使用 PRG 模式。