第二个AJAX Post上的ViewModel为null

时间:2017-10-31 10:52:10

标签: c# ajax asp.net-mvc-5

我有PartialView显示定价信息并允许用户输入promocode。当第一次单击_ListItem.cshtml上的按钮时,HttpPost方法设法获取OrderDetailsViewModel.Details并在控制器中处理。在第二次单击按钮时,它无法捕获模型中的数据。

First Post

Second Post

这是我的代码。

NewPurchaseViewModel

public class NewPurchaseViewModel{
    public NewPurchaseViewModel()
    {
        Details = new OrderDetailsViewModel();
        Shipping = new Address();
        Billing = new Address();
    }
   public string Email { get; set; }
   public string Name { get; set; }
   ...
   public OrderDetailsViewModel Details { get; set; }
}

OrderDetailsViewModel

public class OrderDetailsViewModel{
  public OrderDetailsViewModel()
  {
        Items = new List<ItemsOrderViewModel>();
        Shipping = 0.00M;
  }
  public string Promocode { get; set; }
  public string SubTotal{ get; set; }
  public string Total{ get; set; }
  public string Discount{ get; set; }
  public string Shipping{ get; set; }
  public List<ItemsOrderViewModel> Items { get; set; }
}

Info.cshtml

@model ~.ViewModels.NewPurchaseViewModel
...
@Html.Partial("~/Views/CheckOut/_ListItem.cshtml", Model.Details, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Details"} })

_ListItem.cshtml

@model Nutric.hive.eStore.ViewModels.OrderDetailsViewModel
@using (Ajax.BeginForm("Verify", "PromoCode", null, new AjaxOptions
{
    HttpMethod = "POST",
    UpdateTargetId = "divPromoCode",
    InsertionMode = InsertionMode.Replace
}))
{
    <table>
    ...
    <tbody>
    @for (var i = 0; i < Model.Items.Count; i++)
    {
        <tr>
            <td>@Model.Items[i].ProductPackage.Name</td>
            <td class="qty">@Model.Items[i].Quantity</td>
            <td class="price">@Model.Items[i].ItemTotal</td>
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.Id)
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.Name)
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.Description)
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.ImageURL)
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.Status)
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.RetailPrice)
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.TaxCode)
            @Html.HiddenFor(Model => Model.Items[i].ProductPackage.PackageCode)
            @Html.HiddenFor(Model => Model.Items[i].Quantity)
            @Html.HiddenFor(Model => Model.Items[i].ItemTotal)
        </tr>
    }
    ...
    <tr>
        <td colspan="3">
            <div class="col-lg-10">
                <div class="input-group">
                    @Html.TextBoxFor(Model => Model.Promocode, new { @class = "form-control", maxlength = 15, placeholder = "Type Voucher Code Here...", type = "text" })
                    <span class="input-group-btn">
                        <input type="submit" class="btn btn-secondary" value="Apply !">
                    </span>
                </div>
             </div>
         </td>
    </tr>
    </tbody>
    </table>
    @Html.HiddenFor(Model => Model.Discount)
    @Html.HiddenFor(Model => Model.Shipping)
    @Html.HiddenFor(Model => Model.Subtotal)
    @Html.HiddenFor(Model => Model.Total)
}

@section Script{

    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jquery")
}

PromoCodeController.cs

[HttpPost]
public ActionResult Verify(NewPurchaseViewModel postModel)
{
    OrderDetailsViewModel odvm = postModel.Details;
    //some logic here to check the promo code.
    return PartialView("~/Views/CheckOut/_ListItem.cshtml",odvm );
}

0 个答案:

没有答案