2016-12-24 4 views
0

Ich habe den View-Code geändert, aber es füllt immer noch nicht die Fonds-ddl. Kann jemand sehen, was das Problem ist?2. ddl füllt sich nicht richtig


Dieser Code führt ClientInvestmentsSince2 über Json nennen, das Verfahren in dem Code Bericht Controller ausführt vollständig. Wenn die Steuerung zur Ansicht zurückkehrt, bin ich mir nicht sicher, was passiert. Der Code antwortet auf einen ausgewählten Artikel des Kunden-ddl und soll das Fonds-ddl mit den Mitteln füllen, die der ausgewählte Kunde hält. Etwas ist fischig ... Die Alarme werden nicht alle ausgelöst, nachdem ich den Client ausgewählt habe. alert ("here2") ist aber nicht alert ("here3") oder alert ("here4").

Können Sie helfen? Vielen Dank.

**************************** hier ist der Ansicht Code ************** *************************

@model StockHoldings.Models.Investments 

<head> 
    <script src="jquery-3.1.1.min.js"></script> 
    @*<script src="http://code.jquery.com/jquery-1.10.1.min.js" type='text/javascript'></script>*@ 
</head> 

@using (Html.BeginForm()) 
{ 
    <br /> 
    <br /> 
    <br /> 
    <br /> 
    <br /> 
    <br /> 
    <br /> 

    @Html.DropDownList("Client", ViewData["client"] as SelectList, "Select Client", new { id = "Client_ID", style = "width: 150px;" })<br /> 
    @*{ @Html.DropDownList("Fund", "Select Fund")};*@ 
     <select id="Fund" name="Fund" , style="width: 150px;"></select><br /> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.PurchaseDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.PurchaseDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PurchaseDate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

} 

@Scripts.Render("~/bundles/jquery") 
<script type="text/jscript"> 
    $(document).ready(function() {  
     $('#Client_ID').change(function() 
     { 
      alert('here'); 

      $.getJSON('@Url.Action("ClientInvestmentsSince2", "Reports")', { id: $('#Client_ID').val() }, function (data) 
      { 
       var items = '<option>Select Fund</option>'; 
       $.each(data, function (i, Fund) 
       { 
        items += "<option value='" + Fund.i + "'>" + Fund.val + "</option>"; 
       }) 
       .fail(function (jqxhr, textStatus, errorMessage) { alert(errorMessage); }); 

       //assign the result to the Fund selectlist 
       $('#Fund').html(items); 
      }); 
      alert('here2'); 
     }); 
     alert('here3'); 
    }); 


**************** *********************** hier ist der ReportController Code *********************** **********

using System.Collections.Generic; 
using System.Web.Mvc; 
using System.Data.SqlClient; 

namespace StockHoldings.Controllers 
{ 
    public class ReportsController : Controller 
    { 

     // GET: Reports/ClientInvestmentsSince 
     public ActionResult ClientInvestmentsSince() 
     { 
      string str = @"Data Source=USER\SQLEXPRESS;Initial Catalog=HoldingsConnectionString2;Integrated Security=True"; 
      SqlConnection con = new SqlConnection(str); 
      string query = "select ID, LastName from Clients"; 
      SqlCommand cmd = new SqlCommand(query, con); 
      con.Open(); 
      SqlDataReader rdr = cmd.ExecuteReader(); 
      List<SelectListItem> li = new List<SelectListItem>(); 
      while (rdr.Read()) 
      { 
       li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() }); 
      } 
      ViewData["client"] = li; 

      return View(); 
     } 


     public JsonResult ClientInvestmentsSince2(int Id) 
     { 
      string str = @"Data Source=USER\SQLEXPRESS;Initial Catalog=HoldingsConnectionString2;Integrated Security=True"; 
      SqlConnection con = new SqlConnection(str); 
      string query = "select ID, Fund from Investments where Client_ID = " + Id; 
      SqlCommand cmd = new SqlCommand(query, con); 
      con.Open(); 
      SqlDataReader rdr = cmd.ExecuteReader(); 
      List<SelectListItem> li = new List<SelectListItem>(); 
      while (rdr.Read()) 
      { 
       li.Add(new SelectListItem { Text = rdr["ID"].ToString(), Value = rdr["Fund"].ToString() }); 
      } 
      ViewData["Fund"] = li; 

********************************************* 

// ViewData["Fund"] is loading correctly, and there is a clean exit of the method. 


      return Json(li, JsonRequestBehavior.AllowGet); 
     } 

} 
} 

Antwort

0
 $(document).ready(function() { 
     $('#Client_ID').change(function() { 
      alert('here'); 

      $.getJSON('@Url.Action("ClientInvestmentsSince2", "Reports")', { id: $('#Client_ID').val() }, function (data) { 
       //On client DDL change you have to fill second drop down list right so after called a method it willl 
       //return json here and you can then play with that json to fill your second DDL 
      }); 
      alert('here2'); // it will fire when Client ddl change 
     }); 
     alert('here3');// it will fire once when there is page load 
     }); 
     alert('here4');//it will never fire as it's not in ready function 

Bitte meine Kommentare zu Ihrem Skript finden gab es einige Syntaxfehler i geklärt sind und einige nützliche Kommentare, um zu verstehen, wie diese Funktion funktioniert, hoffe es hilft

Verwandte Themen