2017-10-10 40 views
0

我正在面临mvc 4中的问题,我创建了动作方法,其中我在数据库中插入数据,然后返回模型到视图但模型数据不绑定到隐藏的字段像Id和证件号码, 在其他领域的数据绑定正确,但问题是与这些Id和VoucherNum,Id是主键和VoucherNum是唯一的。 我已经提到了代码和html。模型数据没有绑定到MVC插入后的文本框

代码:

[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>

Form View after insertion

inspected html of those textfields

回答

1

因为辅助方法将首先查找模型状态字典以填充输入的值。模型状态字典在保存VoucherNum之前,当前的值为空值,辅助方法将使用此值生成输入字段的值。

为了解决这个问题,你可以明确地清除模型状态字典返回视图之前,

db.Payments.Add(payment); 
db.SaveChanges(); 

ModelState.Clear(); 

var p = db.Payments.Find(model.Id); 
return View(p); 

或者甚至更​​好,按照PRG模式。通过Post-Redirect-Get模式,在成功更新数据库之后,您将返回一个重定向响应回客户端,并且客户端将向服务器发出全新的GET请求。

对于您的情况,您可以使用RedirectToAction返回302响应。

db.SaveChanges(); 
return RedirectToAction("Edit",new {id=payment.Id}); 

这将告诉浏览器发出Edit与请求URL的新id动作方法的新的GET请求。您的GET操作方法将使用此ID并从数据库获取实体并将其返回。

public ActionResult Edit(int id) 
{ 
    var model = db.Payments.Find(id); 
    return View(model); 
} 

我强烈建议你使用PRG模式。

+0

我没有任何其他行动方法@Shyju –