0

我有显示数据库数据的下拉列表。我在下拉列表中获取数据库数据,但是在下拉列表中选择值后,我想在控制器中传递所选值,然后我想显示该所选值的所有详细数据。但是单击提交按钮,下拉列表的选定值不会传递给控制器​​。

这是我的代码

查看:Index.aspx

<%@ 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">
    SearchIndex
</asp:Content>

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

<h2>SearchIndex</h2>

    <%--<div id = "SelectDoctors">
          <% Html.RenderPartial("SelectDoctors"); %> 
    </div>--%>
    <script type="text/javascript">
        function TestFun() {
            var mdlno = $("#ddlMDLNo").val();
//            $.getJSON("/SearchMDLNo/getMDLNoDetails/" + mdlno, null, function (data) {

//                alert(data.Request_For_Id);
//            });
            var txtmdlno = document.getElementById("Request_For_Id");
            txtmdlno.value = mdlno;
            alert(txtmdlno.value);
        }
</script>

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

         <%: Html.ValidationSummary(true, "Profile Updation was unsuccessful. Please correct the errors and try again.") %>

        Select MDLno 

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

         <input type="submit" value="search" name="SearchMDLNo" />    
    <% } %>
</div>


<div> <% Html.RenderPartial("ListDoctors"); %></div>

</asp:Content>

控制器:SearchMDLNoController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ApricaCRMEvent.Models.CRM.DataLayer;
using ApricaCRMEvent.Models.CRM.DatabaseEntities;

namespace ApricaCRMEvent.Controllers
{
    public class SearchMDLNoController : Controller
    {
        CRM_Doctor_Request mdlnoObj = new CRM_Doctor_Request();

        public ActionResult Index()
        {
            ViewData["MDLno"] = new SelectList(SearchMDLNoDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");
            return View();
        }

        [HttpPost]
        public ActionResult Index(CRM_Doctor_Request model)
        {
            ViewData["MDLno"] = new SelectList(SearchMDLNoDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");

            //CRM_Doctor_Request mdlnoObj = SearchMDLNoDL.getMDLData("GJ04_D023");
            //mdlnoObj = SearchMDLNoDL.getMDLData(model.Request_For_Id);
            return View();
        }
        public ActionResult getMDLNoDetails(string id)
        {
            mdlnoObj=SearchMDLNoDL.getMDLData(id);
            return View();
        }
        public ActionResult ListDoctors()
        {
            return View(mdlnoObj);
        }

    }
}

这是部分视图,在此视图中选择下拉值 .. 将显示详细数据

局部视图:ListDoctors.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ApricaCRMEvent.Models.CRM.DatabaseEntities.CRM_Doctor_Request>" %>


<div><%: Html.DisplayFor(m => m.Request_For_Id) %></div>

<div>
<table>
    <tr>
        <th>
            Request_For_Id
        </th>
        <th>
            Territory
        </th>
        <th>
            Estimated_Amount
        </th>
        <th>
            Actual_Amount
        </th>
        <th>
            Date_Created
        </th>
        <th>
            Compute_CRM_State
        </th>
        <th>
            Compute_Event_Type
        </th>

    </tr>


    <tr>
        <td>
            <%: Html.DisplayFor(m => m.Request_For_Id) %>
        </td>
        <td>
            <%: Html.DisplayFor(m => m.Territory) %>
        </td>
        <td>
            <%: Html.DisplayFor(m => m.Estimated_Amount) %>
        </td>
        <td>
            <%: Html.DisplayFor(m => m.Actual_Amount) %>
        </td>
        <td>
            <%: Html.DisplayFor(m => m.Date_Created) %>
        </td>
        <td>
            <%: Html.DisplayFor(m => m.Compute_Service_State) %>
        </td>
        <td>
            <%: Html.DisplayFor(m => m.Compute_Event_Type) %>
        </td>
      </tr>


</table>

</div>

通过在下拉列表中选择值并单击提交按钮值未通过。在 Index.aspx 中,我尝试了一些 javascript 代码,但它不起作用。


我在代码中改变了一些东西。现在这正在起作用。

查看:Index.aspx

<%@ 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">
    SearchIndex
</asp:Content>

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

<h2>SearchIndex</h2>

    <%--<div id = "SelectDoctors">
          <% Html.RenderPartial("SelectDoctors"); %> 
    </div>--%>
    <script type="text/javascript">
        function TestFun() {
            var mdlno = $("#ddlMDLNo").val();

            var txtmdlno = document.getElementById("Request_For_Id");
            txtmdlno.value = mdlno;
            alert(txtmdlno.value);

        }
</script>

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

         <%: Html.ValidationSummary(true, "Profile Updation was unsuccessful. Please correct the errors and try again.") %>

        Select MDLno 

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

         <input type="submit" value="search" name="SearchMDLNo" />    
    <% } %>
</div>


<%--<div> <% Html.RenderPartial("ListDoctors"); %></div>--%>
<div>
    <fieldset>
    <legend>CRM_Doctor_Request</legend>

    <div class="display-label">Territory</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Territory) %>
    </div>

    <div class="display-label">CRM_Request_For_Type_Id</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.CRM_Request_For_Type_Id) %>
    </div>

    <div class="display-label">Request_For_Id</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Request_For_Id) %>
    </div>

    <div class="display-label">Requestor</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Requestor) %>
    </div>

    <div class="display-label">Division_Id</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Division_Id) %>
    </div>

    <div class="display-label">Suggester</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Suggester) %>
    </div>

    <div class="display-label">Estimated_Amount</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Estimated_Amount) %>
    </div>

    <div class="display-label">Actual_Amount</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Actual_Amount) %>
    </div>

    <div class="display-label">Priority</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Priority) %>
    </div>

    <div class="display-label">CRM_Service_Type_Id</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.CRM_Service_Type_Id) %>
    </div>

    <div class="display-label">Deadline</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Deadline) %>
    </div>

    <div class="display-label">Executed_Date</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Executed_Date) %>
    </div>

    <div class="display-label">Entry_Done_By</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Entry_Done_By) %>
    </div>

    <div class="display-label">Date_Created</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Date_Created) %>
    </div>

    <div class="display-label">Date_Modified</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Date_Modified) %>
    </div>

    <div class="display-label">Event_Type_Id</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Event_Type_Id) %>
    </div>

    <div class="display-label">Compute_Event_Type</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Compute_Event_Type) %>
    </div>

    <div class="display-label">CRM_State_Id</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.CRM_State_Id) %>
    </div>

    <div class="display-label">Compute_CRM_State</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Compute_CRM_State) %>
    </div>

    <div class="display-label">Service_State_Id</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Service_State_Id) %>
    </div>

    <div class="display-label">Compute_Service_State</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Compute_Service_State) %>
    </div>

    <div class="display-label">Is_Active</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Is_Active) %>
    </div>

    <div class="display-label">Comment</div>
    <div class="display-field">
        <%: Html.DisplayFor(model => model.Comment) %>
    </div>
</fieldset>


</div>
</asp:Content>

控制器:SearchMDLNoController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ApricaCRMEvent.Models.CRM.DataLayer;
using ApricaCRMEvent.Models.CRM.DatabaseEntities;

namespace ApricaCRMEvent.Controllers
{
    public class SearchMDLNoController : Controller
    {
        CRM_Doctor_Request mdlnoObj = new CRM_Doctor_Request();

        public ActionResult Index()
        {
            ViewData["MDLno"] = new SelectList(SearchMDLNoDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");
            return View();
        }

        [HttpPost]
        public ActionResult Index(CRM_Doctor_Request model)
        {
            ViewData["MDLno"] = new SelectList(SearchMDLNoDL.getAllMDLno(), "Request_For_Id", "Request_For_Id");

            //CRM_Doctor_Request mdlnoObj = SearchMDLNoDL.getMDLData("GJ04_D023");
            //mdlnoObj = SearchMDLNoDL.getMDLData(model.Request_For_Id);
            mdlnoObj = SearchMDLNoDL.getMDLData(model.Request_For_Id);
            return View(mdlnoObj);
        }
    }
}
4

1 回答 1

0

试试这个 :

$.getJSON("/SearchMDLNo/getMDLNoDetails",{id : mdlno}, function (data) {
         alert(data.Request_For_Id);
});

这应该适合你


<script type="text/javascript">
        function TestFun() {
            var mdlno = $("#ddlMDLNo").val();
            $.ajax({
              type: 'POST',
              dataType: 'json',
              data: {id: mdlno},
              success: function(result){
                      alert('success');
              }
            });
        }
</script>

然后在你的控制器中改变它:

    [HttpPost]
    public ActionResult getMDLNoDetails(string id)
    {
        mdlnoObj=SearchMDLNoDL.getMDLData(id);
        return Json(true);
    }
于 2013-01-31T10:42:10.603 回答