2017-04-02 9 views
-1

私は選択された状態に基づいて都市のリストを絞り込む一連のカスケードドロップダウンをしようとしています。これまでのところ私はこれを持っている: ビュー:Asp.NetのためのカスケードDropdownList面倒なWeb MVC

<div class="form-group"> 
      @Html.LabelFor(model => model.CollLocation, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       <div class="col-md-6"> 
        @Html.DropDownList("stateCol", null, htmlAttributes: new { @class = "form-control" }, optionLabel: "Select a state") 
        @Html.ValidationMessageFor(model => model.CollLocation, "", new { @class = "text-danger" }) 
       </div> 
       <div class="col-md-6"> 
        @Html.DropDownList("CollLocation", null, htmlAttributes: new { @class = "form-control" }, optionLabel: "Select a city") 
        @Html.ValidationMessageFor(model => model.CollLocation, "", new { @class = "text-danger" }) 
       </div> 

そして、このコントローラ:私は郵便番号、primary_city、および状態を持っている私のジップコードモデルで

// GET: Coll/Create 
public ActionResult Create() 
{ 
    var stateColl = db.ZipCodes.OrderBy(c => c.state).Select(c => c.state).Distinct(); 

    var cityCol = db.ZipCodes.Select(C => C.primary_city).Distinct(); 

    ViewBag.stateCol = new SelectList(stateColl); 
    ViewBag.ArRecID = new SelectList(db.ArRecs, "ArRecID", "ArZipID"); 
    ViewBag.CollLocation = new SelectList(cityCol); 
    return View(); 
} 

// POST: Coll/Create 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "CollID,ArRecID,CollName,CollDescr,CollValue,CollOwner,CollLocation,DateCreated,ModBy,ModDate,CreatedBy")] Collateral collateral) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Coll.Add(coll); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.ArRecID = new SelectList(db.ArRecs, "ArRecID", "ArZipID", coll.ArRecID); 
    ViewBag.CollLocation = new SelectList(db.ZipCodes, "zip", "primary_city", coll.CollLocation).Distinct(); 
    return View(collateral); 
} 

を。 CollLocationでは、stateColドロップダウンで選択された状態にある引用のみを表示したいと考えています。両方のドロップダウンは私のために働くが、彼らは一緒に働いていない。私は他のチュートリアルと回答を試みましたが、彼らは私の頭をさらに傷つけました。どんな助けでも大歓迎です。

+2

はあなたのコードを勉強オススメ[this DotNetFiddle](https://dotnetfiddle.net/1bPZym) - これにはAjaxが必要ですが、コードには他にも複数の問題があります –

+0

ありがとうございました。それは素晴らしいフィドルです。それは私の質問の多くを助け、清算しています – TomBB

答えて

1

州のドロップダウンを変更するたびに、Ajaxリクエストを行う必要があります。したがって、Stateドロップダウンが変更された場合、JavaScript関数はAjaxリクエストをコントローラに送り、必要なZipcodeを問い合わせる必要があります。

あなたはGithubの中で私のプロジェクトをダウンロードすることができますし、見ている2つのファイル "\ビュー\ ClientOrder \ Create.cshtml" と "\ RiceStoreScripts \ ClientOrder.js"

hongyichao/MVCRiceStore

関連する問題