【问题标题】:Submit Button is not working In Razor asp.net mvc提交按钮在 Razor asp.net mvc 中不起作用
【发布时间】:2019-05-07 19:24:20
【问题描述】:

我有一个 ViewModel、Controller 和 View,如下所示

 public class H2HViewModel
{
    public DateTime EffectiveDate { get; set; }
    public byte TransferMethod { get; set; }
    public string ListOfSelectedBatchID { get; set; }

}

控制器

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Create(H2HViewModel model)
 {
   var detailPayments = db.Payments.Where(x => x.Status == 2).ToList();
   foreach (Payment detail in detailPayments)
   {
       detail.PaymentStatus = 4; 
       detail.EffectiveDate = model.EffectiveDate;
       detail.TransferMethod = model.TransferMethod.ToString();
   }
   db.SaveChanges();
   return RedirectToAction("Dashboard", "User");
 }

查看

@model EPayment.Data.ViewModels.H2HViewModel
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.EditorFor(model => model.EffectiveDate, new { @class = "form-control dtpicker" })
    <div class="col-md-10">
            @Html.RadioButtonFor(model => model.TransferMethod, "AB", new { htmlAttributes = new { @class = "form -control" } }) AB
            @Html.RadioButtonFor(model => model.TransferMethod, "BC", new { htmlAttributes = new { @class = "form -control" } }) BC

    </div>
   <input type="submit" value="Submit" class="btn btn-default"/>
}

当我点击提交按钮时,没有任何反应, 我在 ActionResult Create 上设置了一个断点来调试它,但也没有任何反应。 我参考了这个stackoverflow.com/questions/16443927,并且已经做到了,但是没有用,知道吗?谢谢。

【问题讨论】:

  • 您的 post 方法上没有 [ValidateAntiForgeryToken],并且您的 post 方法不返回任何数据!
  • 对不起,我已经更新了@LazZiya上面的代码

标签: c# asp.net-mvc razor


【解决方案1】:

您没有指定该表单的发布位置...Html.BeginForm 可以采用several parameters。您应该指定 ActionNameControllerName

@using (Html.BeginForm("Create", "Controller", FormMethod.Post))

注意:“Controller”将是您的控制器的名称。

【讨论】:

  • @chaeusangchen 你能发布你的 HTML 的其余部分吗?您的模型可能没有正确反序列化。你会得到什么类型的回复?
  • 嗨@maxshuty,我已经在上面的代码中更新了我的html。我没有得到任何回应
猜你喜欢
  • 2013-05-02
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
相关资源
最近更新 更多