gpt4 book ai didi

asp.net-mvc - 如何防止页面被刷新?

转载 作者:行者123 更新时间:2023-12-04 05:11:27 30 4
gpt4 key购买 nike

我有 DropDownList和我页面上的提交按钮。 DropDownList有来自数据库的数据列表,在从下拉列表中选择值然后单击提交按钮时,我在主视图页面的部分 View 中获取所选下拉列表值的记录数。我的代码给出了正确的输出。我已经通过模型将 View 绑定(bind)到 Controller 。使用 html.hiddenfor。
但是每当我像往常一样点击提交按钮时,我的整个页面都会刷新。但我只需要刷新部分 View 而不是整个页面。

这是我的代码,它工作正常。但是通过这段代码,我的整个页面都在刷新。我想阻止它。 :

查看:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ApricaCRMEvent.Models.CRM.DatabaseEntities.CRM_Doctor_Request>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MDLNoDDLIndex
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script type="text/javascript">
function TestFun() {
var mdlno = $("#ddlMDLNo").val();
var txtmdlno = document.getElementById("Request_For_Id");
txtmdlno.value = mdlno;
}
</script>

<div>
<h2>Search by MDLNo</h2>
<% using (Html.BeginForm())
{ %>

Select MDLno

<%= Html.DropDownList("ddlMDLNo", ViewData["MDLno"] as SelectList, "--Select One--", new { onchange = "TestFun()" })%>
<%: Html.HiddenFor(model => model.Request_For_Id) %>

<input type="submit" value="search" name="SearchMDLNo" id="btnclick" />
<% } %>
</div>
<div id="showtable"> //partial view
<% if (ViewBag.load == true)
{ %>
<%Html.RenderAction("MDLNoDataList"); %>
<% } %>
</div>

</asp:Content>

Controller :
// 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));
}

最佳答案

您可以使用 jQuery 为您执行此操作。在 jQuery 中捕获表单提交,而不是通过浏览器执行完整的表单发布,而是使用 jQuery 的 .ajax() 方法将表单数据提交到 Controller 操作。

像这样的东西:

$.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
}
});

关于asp.net-mvc - 如何防止页面被刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14873724/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com