验证后丢失表单值

时间:2015-07-23 18:30:16

标签: asp.net-mvc validation viewmodel

使用ViewModel进行验证:

public async Task<ActionResult> Pay(FormCollection form, CCvm model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
}

我的控制器在帖子上调用任务:

@model GCwholesale.Models.CCvm
@{
    Layout = "~/Views/Shared/_HomeSubPageLayout.cshtml";
    ViewBag.Title = "Secure Checkout";
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
      <div class="Payment">
         <label>Name on Card: </label>
         @Html.EditorFor(model => model.cardHolderName, new { htmlAttributes = new { @placeholder = "Cardholder Name Please", @Value = ViewBag.Name } })<br />
         @Html.ValidationMessageFor(model => model.cardHolderName)
       <button class="submitCheckout">SUBMIT NOW</button>
      </div>
}

观点:

Not enough input arguments

但是当验证失败时,表格中的数据就会消失。

谢谢你看看。

2 个答案:

答案 0 :(得分:1)

您无需在 EditorFor 中设置@Value = ViewBag.Name

@Html.EditorFor(model => model.cardHolderName, 
new { htmlAttributes = new { @placeholder = "Cardholder Name Please" } })

此外,您不需要 FormCollection 作为参数,因为您已经拥有 CCvm模型

public async Task<ActionResult> Pay(CCvm model){
{
   //...
}

答案 1 :(得分:0)

@Value = ViewBag.Name

您未设置ViewBag.Name,因此它不会有值并会导致输入空白。删除它,让HtmlHelper根据发布的模型中的值设置它。