2013-02-14 167 views
1

我有我的网页DropDownList和一个提交按钮。 DropDownList有来自数据库的数据列表,并从下拉列表中选择值,然后单击提交按钮,我在主视图页面的局部视图中获取所选下拉列表值的记录数。我的代码给出了正确的输出。我通过模型将View绑定到控制器。使用html.hiddenfor。 但是,每当我点击提交按钮像往常一样,我的整个页面得到刷新。但我只需要刷新部分视图而不是整个页面。如何防止页面被刷新?

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

观点:

<%@ 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> 

控制器:

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

回答

2

您可以使用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 
    } 
}); 
+0

但是当我使用AJAX我就不是能够通过列表下拉到控制器的设定值。您能否请我建议我如何将选定的下拉列表值通过与我在代码中完成的方式不同的另一种方式传递给控制器​​。 – 2013-02-15 04:59:50

+0

@anshu ddlMDLNo在ajax请求中传递,请参阅M.Ob代码的第4行。 – 2013-02-15 08:07:32