2013-02-06 126 views
0

C#是不是我的日常語言,所以很新,它創建HTML下拉列表C#

這是我的模型

public class SpecialtyListsModels 
{ 
    public Dictionary<int, dynamic> specialties = new Dictionary<int, dynamic>(); 

    public Dictionary<int, dynamic> SpecialtyLists() 
    { 
     specialties.Add(1, new {name = "Varun", age = "11"}); 
     specialties.Add(2, new {name = "Khushi", age = "12"}); 

     return (specialties); 
    } 
} 

這是我

public class SignupController : Controller 
{ 
    public ActionResult Index() 
    { 
     PFSignup.Models.SpecialtyListsModels objSpecialtyList = new PFSignup.Models.SpecialtyListsModels(); 

     Dictionary<int, dynamic> specialtyLists = objSpecialtyList.SpecialtyLists(); 

     return View(); 
    } 

    [HttpPost] 
    public ActionResult Create(SignupModels signup) 
    { 
     return View(signup); 
    } 
} 

這是我在查看我想添加所有特色的下拉列表

<body> 
<div> 
    @using (Html.BeginForm("Create", "Signup")) 
    { 
     <label for="first_name">First Name:</label> 
     <input type="text" name="first_name" id="first_name" value="" /> 

     <br /> 

     <label for="last_name">Last Name:</label> 
     <input type="text" name="last_name" id="last_name" value ="" /> 

     @Html.DropDownList("specialty_list", new SelectList(SpecialtyListModels,) 

     <input type="submit" name="submit" value="Submit" /> 
    } 
</div> 

是否有人可以幫助我的下拉列表中的代碼,做我需要在視圖的頂部或東西

+0

可以先發一個模型視圖並使用該模型提供下拉條目 –

+0

您可以幫我一些文檔或示例代碼嗎? – user1833222

+0

查看我的答案 –

回答

0

您可能需要先使用該鏈接的文檔添加列表:http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc

public class SpecialtyListsModels { public Dictionary specialties = new Dictionary();

public Dictionary<int, dynamic> SpecialtyLists() 
    { 
     specialties.Add(1, new {name = "Varun", age = "11"}); 
     specialties.Add(2, new {name = "Khushi", age = "12"}); 

     return (specialties); 
    } 
} 

這是我

public class SignupController : Controller 
{ 
    public ActionResult Index() 
    { 
     PFSignup.Models.SpecialtyListsModels objSpecialtyList = new PFSignup.Models.SpecialtyListsModels(); 

     Dictionary<int, dynamic> specialtyLists = objSpecialtyList.SpecialtyLists(); 

     return View(specialtyLists); 
    } 

    [HttpPost] 
    public ActionResult Create(SignupModels signup) 
    { 
     return View(signup); 
    } 
} 

這是我的看法中,我要添加的所有特色的下拉列表

@model Dictionary<int, dynamic> 
    <body> 
    <div> 
     @using (Html.BeginForm("Create", "Signup")) 
     { 
      <label for="first_name">First Name:</label> 
      <input type="text" name="first_name" id="first_name" value="" /> 

      <br /> 

      <label for="last_name">Last Name:</label> 
      <input type="text" name="last_name" id="last_name" value ="" /> 

      @Html.DropDownList("specialty_list", new SelectList(Model) 

      <input type="submit" name="submit" value="Submit" /> 
     } 
    </div> 
</body>