【问题标题】:Ajax.BeginForm render wrong url in mvcAjax.BeginForm 在 mvc 中呈现错误的 url
【发布时间】:2017-06-21 12:59:19
【问题描述】:

我正在尝试使用 ajax 而不是 post 方法的整体视图来加载 div 数据。 但它会在发布操作时返回 object%20HTMLInputElement 操作名称。

控制器:

 [HttpGet]
 public ActionResult Index()
 {
   return View();
 }

 [HttpPost]
 public ActionResult Index(DemoCLass objdemo)
 {
   return View();
 }

查看

<div id="divEmp">
@using (Ajax.BeginForm("Index", "Challan", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp" }))
{
    @Html.AntiForgeryToken()
    <h3 style="text-align:center;" class="row header">Challan Data</h3>

    @Html.Partial("_DateCommonFT")
            }

它包括_Layout.cshtml 我将脚本定义为:

<script src="~/Scripts/jquery-1.12.4.min.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

如何使用 ajax 在发布请求时仅呈现发布操作而不加载整个页面 (_layout.cshtml)。

【问题讨论】:

  • 对不起!您面临的问题是什么?我仍然无法理解您当前和预期的行为。
  • 关闭您的 div 标签,例如
  • @Priya 使用错误消息/意外行为和预期输出更新您的问题。
  • @Priya 你解决问题了吗?

标签: c# jquery ajax asp.net-mvc razor


【解决方案1】:

您可以尝试关闭您的 div 标签并在控制器中接收HtmlForgeryToken,如下所示。

您还可以通过在 Index 方法中返回 PartialView() 来使用 PartialView 填充目标 div

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(DemoCLass objdemo)
{
   return PartialView();
}


<div id="divEmp">
</div>
@using (Ajax.BeginForm("Index", "Challan", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp" }))
{
    @Html.AntiForgeryToken()
    <h3 style="text-align:center;" class="row header">Challan Data</h3>

    @Html.Partial("_DateCommonFT")
}

【讨论】:

    【解决方案2】:

    请在 Ajax.Begin 表单中使用 OnSuccess 方法。

    在视图中:-

     @using (Ajax.BeginForm("Index", "Challan", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp",  OnSuccess = "AjaxForm" }))
        {
        }
    

    在脚本中:- 这里从 post 控制器返回 json。

    function AjaxForm(response){
    .....do as uou want...
    }
    

    在控制器中:-

    [HttpPost]
     public ActionResult Index(DemoCLass objdemo)
     {
       return json(new {IsSuccess = true},JsonRequestBehavior.AllowGet);
     }
    

    如果您对此有任何疑问,请告诉我

    【讨论】:

      【解决方案3】:

      使用PartialView 方法返回没有布局的视图。

      [HttpPost]
      public ActionResult Index(DemoCLass objdemo)
      {
         return PartialView();
      }
      

      如果您只想为 ajax 表单提交返回没有布局标记的 html,您可以检查请求标头以查看请求是否为 xhr 请求。 Request.IsAjaxRequest() 方法在这里会很方便。

      [HttpPost]
      public ActionResult Index(DemoCLass objdemo)
      {
         if (Request.IsAjaxRequest())
         {
            return PartialView();
         }
         else
         {
            return View();
         }
      }
      

      【讨论】:

      • 如果我的 ajax 帖子没有点击控制器帖子方法怎么办?它的网址错误object%20HTMLInputElement
      • 检查页面的视图源,看看为什么它会为表单生成错误的操作属性值。我的假设是_DateCommonFT 部分视图呈现的代码破坏了标记。您可以尝试评论一下并尝试吗?这将帮助您隔离问题
      • 我通过评论部分视图进行了检查,仅包含一个用于控制发布操作的按钮。但没有运气.. 渲染object%20HTMLInputElement
      • 我希望你修复了你的 div "divEmp" 缺少的结束标签。我(仅)尝试了您在问题中发布的代码,它对我来说效果很好。
      • @Shyju 我认为 OP 也错过了控制器中的 [ValidateAntiForgeryToken]
      猜你喜欢
      • 2017-01-28
      • 2016-09-17
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多