[英]Ajax.BeginForm render wrong url in mvc
我試圖使用ajax加載div數據,而不是post方法的整個視圖。 但它在后操作中返回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標簽並在控制器中接收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")
}
請在Ajax.Begin表格中使用OnSuccess方法。
在以下地區:-
@using (Ajax.BeginForm("Index", "Challan", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp", OnSuccess = "AjaxForm" }))
{
}
在腳本中:-在此從發布控制器返回json。
function AjaxForm(response){
.....do as uou want...
}
在控制器中:
[HttpPost]
public ActionResult Index(DemoCLass objdemo)
{
return json(new {IsSuccess = true},JsonRequestBehavior.AllowGet);
}
如果您對此有任何疑問,請告訴我
使用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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.