Asp.net mvc 如何防止页面刷新?
我的页面上有DropDownList和submit按钮。DropDownList拥有数据库中的数据列表,当从下拉列表中选择值,然后单击submit按钮时,我在主视图页面的部分视图中获得所选下拉列表值的记录数。我的代码给出了正确的输出。我已通过模型将视图绑定到控制器。使用html.hiddenfor。 但每当我像往常一样点击提交按钮,我的整个页面就会刷新。但我只需要刷新部分视图,而不是整个页面 这是我的代码,工作正常。但是通过这段代码,我的整个页面都被刷新了。我想阻止它 视图: 控制器:Asp.net mvc 如何防止页面刷新?,asp.net-mvc,reload,page-refresh,asp.net-mvc-partialview,Asp.net Mvc,Reload,Page Refresh,Asp.net Mvc Partialview,我的页面上有DropDownList和submit按钮。DropDownList拥有数据库中的数据列表,当从下拉列表中选择值,然后单击submit按钮时,我在主视图页面的部分视图中获得所选下拉列表值的记录数。我的代码给出了正确的输出。我已通过模型将视图绑定到控制器。使用html.hiddenfor。 但每当我像往常一样点击提交按钮,我的整个页面就会刷新。但我只需要刷新部分视图,而不是整个页面 这是我的代码,工作正常。但是通过这段代码,我的整个页面都被刷新了。我想阻止它 视图: 控制器: 您可以使
您可以使用jQuery来完成这项工作。在jQuery中捕获表单提交,使用jQuery的.ajax方法将表单数据提交给控制器操作,而不是通过浏览器执行完整的表单发布 大概是这样的:
$.ajax({
url: urlToControllerAction,
data: {
ddlMDLNo: ddlMDLNo,
Request_For_Id: Request_For_Id
},
type: 'POST',
success: function (results) {
var partialData = $(results);
$('#showtable').html(partialData);
},
error: function (xhr, ajaxOptions, thrownError) {
// do something
}
});
但当我使用ajax时,我无法将下拉列表的选定值传递给控制器。您能建议我如何将所选下拉列表值以不同于我在代码中所做的方式传递给控制器。@anshu ddlMDLNo是在ajax请求中传递的,请参见M.Ob代码的第4行。
// Search by mdl no
public ActionResult MDLNoDDLIndex()
{
ViewData["MDLno"] = new SelectList(CRMSearchReportDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");
ViewBag.load = false;
return View();
}
[HttpPost]
public ActionResult MDLNoDDLIndex(CRM_Doctor_Request model)
{
ViewData["MDLno"] = new SelectList(CRMSearchReportDL.getAllMDLno(), "Request_For_Id", "Request_For_Id",model.Request_For_Id);
ViewBag.load = true;
return View();
}
public ActionResult MDLNoDataList()
{
List<CRM_Doctor_Request> drlist = new List<CRM_Doctor_Request>();
return PartialView(drlist);
}
[HttpPost]
public ActionResult MDLNoDataList(CRM_Doctor_Request model)
{
return PartialView(CRMSearchReportDL.getMDLNoWiseDetails(model.Request_For_Id));
}
$.ajax({
url: urlToControllerAction,
data: {
ddlMDLNo: ddlMDLNo,
Request_For_Id: Request_For_Id
},
type: 'POST',
success: function (results) {
var partialData = $(results);
$('#showtable').html(partialData);
},
error: function (xhr, ajaxOptions, thrownError) {
// do something
}
});