2011-01-05 12 views
0

別のカテゴリの名前をドロップダウンリストとして表示すると、すべてのカテゴリの選択である一般的な名前が表示されます。検索機能を作成するには、これが必要です。カスタマイズされたドロップダウンリストを表示

orginal categori一覧は次のとおりです。

  • Datorer & IT
  • Filosofi &宗教
  • スポーツ& Fritid
  • Djur &ナチュール
  • Konst &ムジーク
  • Psykolo GI & Pedagogik

ドロップダウンリストとして表示するための要求:

  • すべてのカテゴリー
  • Datorer & IT
  • Filosofi &宗教
  • スポーツ& Fritid
  • Djur &ナチュール
  • Konst &ムジーク
  • Psykologi & Pedagogik

<%インポートネームスペース@ = "BokButik1" %>

制御言語= "C#の" 継承@ <% = "System.Web.Mvc .ViewUserControl」%>

<% using (Html.BeginForm()) {%> 

<fieldset> 
    <legend>Edit Album</legend> 

     <%: Html.DropDownList("KategoriID", new SelectList(ViewData["Kategoris"] as IEnumerable, "KategoriID", "KategoriNamn", Model.Kategoris))%> 



    <p> 
     <input type="submit" value="Save" /> 
    </p> 
</fieldset> 

<% } %> 



namespace BokButik1.ViewModels 
{ 
    public class SokningIndexViewModel 
    { 
     public List<Kategori> Kategoris { get; set; } 
    } 
} 


namespace BokButik1.Controllers 
{ 
    public class SokningController : Controller 
    { 


     private IKategoriRepository myIKategoriRepository = new KategoriRepository(); 

     // 
     // GET: /Sokning/ 

     public ActionResult Index() 
     { 
      var SokningIndexViewModel = new SokningIndexViewModel 
      { 
       Kategoris = myIKategoriRepository.HamtaAllaKategoriNamn()  
      }; 

      return View(SokningIndexViewModel); 
     } 


    } 
} 

答えて

0

ちょうどあなたのリストの先頭に新しいKategori項目を追加、しかし、私tはまた、あなたのビューで

public ActionResult Index() 
{ 

    var SokningIndexViewModel = new SokningIndexViewModel() 
    { 
    Kategoris = myIKategoriRepository.HamtaAllaKategoriNamn();  
    }; 
    //add the 'all catagory' item 
    SokningIndexViewModel.Kategoris.Insert(0, new Kategori() { 
    KategoriID = 0, 
    KategoriNamn = "All Category" 
    }); 
    return View(SokningIndexViewModel); 

} 

まであなたのモデルとのViewDataを混合することができる表示されます

<%: Html.DropDownList("KategoriID", new SelectList(Model.Kategoris as IEnumerable, "KategoriID", "KategoriNamn"))%> 
関連する問題