简体   繁体   English

Ajax.BeginForm在MVC中呈现错误的URL

[英]Ajax.BeginForm render wrong url in mvc

I am trying to load a div data using ajax rather than whole view on post method. 我试图使用ajax加载div数据,而不是post方法的整个视图。 but it returns object%20HTMLInputElement action name on post action. 但它在后操作中返回object%20HTMLInputElement操作名称。

Controller: 控制器:

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

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

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")
            }

It includes _Layout.cshtml where i have defined scripts as: 它包含_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>

How to render only post action without loading whole page (_layout.cshtml) on post request using ajax. 如何使用ajax在发布请求时仅呈现发布操作而不加载整个页面(_layout.cshtml)。

Can you try to close your div tag and receive HtmlForgeryToken in controller like following. 您可以尝试关闭div标签并在控制器中接收HtmlForgeryToken ,如下所示。

you can also fill your target div with PartialView by returning PartialView() in Index method 您还可以通过在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")
}

please Ajax.Begin form you can use OnSuccess method. 请在Ajax.Begin表格中使用OnSuccess方法。

In VIew:- 在以下地区:-

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

In Script:- here return json from post controller. 在脚本中:-在此从发布控制器返回json。

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

in Controller:- 在控制器中:

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

if you have any query in this one then tell to me 如果您对此有任何疑问,请告诉我

Use the PartialView method to return a view without the layout. 使用PartialView方法返回不带布局的视图。

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

If you want to return the html without layout markup only for the ajax form submissions, you can check the request headers to see whether the request is an xhr request or not. 如果只想为ajax表单提交返回不带布局标记的html,则可以检查请求标头以查看该请求是否为xhr请求。 Request.IsAjaxRequest() method will be handy here. Request.IsAjaxRequest()方法在这里很方便。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM