2017-01-12 7 views
0

ソースの選択変更に記入:私はのonchangeイベントが発生しませんguees、http://www.advancesharp.com/blog/1145/mvc-dropdownlistfor-fill-on-selection-change-of-another-dropdownMVC DropDownListForは別のドロップダウン

私の最初のDropDownListが満たされているが、変更した後、第二のDropDownListはまだ空ですか?申し訳ありませんが、私は間違いを見つけることができません。

_Layout.cshtml:

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 

    <title>SCLWEB | @ViewBag.Title</title> 

    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'> 

    <!-- Add local styles, mostly for plugins css file --> 
    @if (IsSectionDefined("Styles")) 
    {@RenderSection("Styles", required: false)} 

    <!-- Add jQuery Style direct - used for jQGrid plugin --> 
    <link href="@Url.Content("~/Scripts/plugins/jquery-ui/jquery-ui.css")" rel="stylesheet" type="text/css" /> 

    <!-- Primary Inspinia style --> 
    @Styles.Render("~/Content/css") 
    @Styles.Render("~/font-awesome/css") 

</head> 
<body> 
    <!-- Wrapper--> 
    <div id="wrapper" class="@Html.PageClass()"> 

     <!-- Navigation --> 
     @Html.Partial("_Navigation") 

     <!-- Page wraper --> 
     <div id="page-wrapper" class="gray-bg"> 

      <!-- Top Navbar --> 
      @Html.Partial("_Navbar") 

      <!-- Main view --> 
      @RenderBody() 

      <!-- Footer --> 
      @Html.Partial("_Footer") 

     </div> 
     <!-- End page wrapper--> 

    </div> 
    <!-- End wrapper--> 

    <!-- Section for main scripts render --> 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/bootstrap") 
    @Scripts.Render("~/plugins/slimScroll") 
    @Scripts.Render("~/bundles/inspinia") 

    <!-- Handler for local scripts --> 
    @RenderSection("scripts", required: false) 

</body> 
</html> 

Index.cshtml

@model Core.Auswahl 

@{ 
    ViewBag.Title = "Index"; 
} 


@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 

     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

     <div class="form-group"> 
      @Html.LabelFor(m => m.Hersteller, new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownListFor(m => m.Hersteller, 
        new SelectList(ViewBag.StateList, "Id", "Symbol"), 
        "Hersteller", new { @class = "form-control", @onchange = "FillCity()" }) 
       @Html.ValidationMessageFor(m => m.Hersteller, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(m => m.Baureihe, new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownListFor(m => m.Baureihe, 
        new SelectList(Enumerable.Empty<SelectListItem>(), "ID", "Symbol"), 
        "Baureihe", 
        new { @class = "form-control" }) 
       @Html.ValidationMessageFor(m => m.Baureihe, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

    </div> 
} 


@section scripts { 
    <script> 
     function FillCity() { 
      var herstellerId = $('#Hersteller').val(); 
      $.ajax({ 
       url: '/Home/FillCity', 
       type: "GET", 
       dataType: "JSON", 
       data: { Hersteller: herstellerId }, 
       success: function (baureihen) { 
        $("#Baureihe").html(""); 
        $.each(baureihen, function (i, baureihe) { 
         $("#Baureihe").append(
         $('<option></option>').val(baureihe.ID).html(baureihe.Symbol)); 
        }); 
       } 
      }); 
     } 
    </script> 
} 

HomeController.cs

using Core; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace SclWeb 
{ 
    public class HomeController : Controller 
    { 
     SclDataEntities sclDataEntities = new SclDataEntities(); 

     // GET: Home 
     public ActionResult Index() 
     { 
      ViewBag.StateList = sclDataEntities.Hersteller; 

      Auswahl auswahl = new Auswahl(); 

      return View(auswahl); 
     } 

     public ActionResult FillCity(int herstellerId) 
     { 
      var baureihen = sclDataEntities.Baureihe.Where(c => c.HerstellerId == herstellerId); 
      return Json(baureihen, JsonRequestBehavior.AllowGet); 
     } 
    } 
} 
+0

ブラウザでF12キーを押し、プルダウン変更後にサーバーから送信される内容を表示します。 –

+0

DOM Explorer/Konsole/Debugger/Netzwerk/"ReaktionsfähigkeitderBenutzeroverfläche"/Profile/Speicher/Emulation? 申し訳ありませんが、私はASP.NET MVCのエキスパートではありません! – MeerArtefakt

+0

いいえ、ChromeまたはFirefox F12で、コントローラから実際に取得されるものをNetWorkで確認します。 –

答えて

0

私は(私はドイツ語を知らない)を理解するとして、あなたはサーバーを得ましたあなたのコントローラをこれに接続してください:

//your viewmodel for dropdown 
public class BaureiheViewModel 
{ 
    public int ID {get; set;} 
    public string Symbol {get; set;} 
} 

public class HomeController : Controller 
{ 
    SclDataEntities sclDataEntities = new SclDataEntities(); 

    // GET: Home 
    public ActionResult Index() 
    { 
     ViewBag.StateList = sclDataEntities.Hersteller; 

     Auswahl auswahl = new Auswahl(); 

     return View(auswahl); 
    } 

    public ActionResult FillCity(int herstellerId) 
    { 
     //check here in debug step by step 
     var baureihen = sclDataEntities.Baureihe.Where(c => c.HerstellerId == herstellerId); 
     var model = new BaureiheViewModel 
     { 
      ID = baureihen.ID, 
      Symbol = baureihen.Symbol 
     } 
     return Json(model, JsonRequestBehavior.AllowGet); 
    } 
} 

breakpoitをコントローラーに置き、何が間違っているかを確認してください。

+0

'リターンJSON(baureihen、JsonRequestBehavior.AllowGet)にonchange' '@を変更;' 'baureihen'は、JSONの結果のために間違ったデータ型であるため。 'baureihen'を連載する必要がありましたか? – MeerArtefakt

+0

@MeerArtefakt yeap。複雑なEntityオブジェクトを送信しようとしているように見えますが、シリアル化できませんでした。あなたがViewModelを送ったらそれはうんざりですか? –

+0

申し訳ありませんはい、私はEntity Frameworkオブジェクトを送信して、BaureiheViewModelのようなViewModelを送信します。 – MeerArtefakt

関連する問題