2012-02-27 59 views
0

下拉我是新来MVC3 我发现很难创建一个dropdown.I已经完成了所有其他相关的问题,走了,但他们都似乎是复杂 我绝对需要创建一个下拉列表,插入数据库中的选定值来填充MVC3

这里是我曾尝试:

//Model class: 
    public int Id { get; set; } 
    public SelectList hobbiename { get; set; } 
    public string filelocation { get; set; } 
    public string hobbydetail { get; set; } 
//Inside Controller 
public ActionResult Create() 
    { 
     var values = new[] 
     { 
      new { Value = "1", Text = "Dancing" }, 
      new { Value = "2", Text = "Painting" }, 
      new { Value = "3", Text = "Singing" }, 
     }; 
     var model = new Hobbies 
     { 
      hobbiename = new SelectList(values, "Value", "Text") 
     }; 
     return View(); 
    } 
//Inside view 
<div class="editor-label"> 
     @Html.LabelFor(model => model.hobbiename) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor( x => x.hobbiename,  Model.hobbiename) 
     @Html.ValidationMessageFor(model => model.hobbiename) 
    </div> 

我得到一个错误:system.missingMethodException而:此对象定义无参数的构造函数

回答

0

我会创造他们作为

型号:

public class ViewModel 
{ 
    public int Id { get; set; } 
    public string HobbyName { get; set; } 
    public IEnumerable<SelectListItem> Hobbies {get;set; } 
    public string FileLocation { get; set; } 
    public string HobbyDetail { get; set; } 
} 

行动

public ActionResult Create() 
{ 
    var someDbObjects= new[] 
    { 
     new { Id = "1", Text = "Dancing" }, 
     new { Id = "2", Text = "Painting" }, 
     new { Id = "3", Text = "Singing" }, 
    }; 
    var model = new ViewModel 
    { 
     Hobbies = someDbObjects.Select(k => new SelectListItem{ Text = k, Value = k.Id }) 
    }; 

    return View(model); 
} 

查看

<div class="editor-label"> 
    @Html.LabelFor(model => model.HobbyName) 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(x => x.HobbyName, Model.Hobbies) 
    @Html.ValidationMessageFor(model => model.HobbyName) 
</div> 
0

您没有传递任何模型到你行动的视角。此外,你不应该使用相同的属性作为DropDownListFor助手的第一和第二个参数。你通过为Lambda表达式的第一个参数对应于您的视图模型中的标量属性,将举行选定值和允许你提交表单时检索此值回。第二个参数是集合。

所以,你能适应一点点代码:

型号:

public class Hobbies 
{ 
    [Required] 
    public string SelectedHobbyId { get; set; } 
    public IEnumerable<SelectListItem> AvailableHobbies { get; set; } 

    ... some other properties that are irrelevant to the question 
} 

控制器:

public class HomeController: Controller 
{ 
    public ActionResult Create() 
    { 
     // obviously those values might come from a database or something 
     var values = new[] 
     { 
      new { Value = "1", Text = "Dancing" }, 
      new { Value = "2", Text = "Painting" }, 
      new { Value = "3", Text = "Singing" }, 
     }; 

     var model = new Hobbies 
     { 
      AvailableHobbies = values.Select(x => new SelectListItem 
      { 
       Value = x.Value, 
       Text = x.Text 
      }); 
     }; 
     return View(model); 
    }  

    [HttpPost] 
    public ActionResult Create(Hobbies hobbies) 
    { 
     // hobbies.SelectedHobbyId will contain the id of the element 
     // that was selected in the dropdown 
     ... 
    } 
} 

查看:

@model Hobbies 
@using (Html.BeginForm()) 
{ 
    @Html.LabelFor(x => x.SelectedHobbyId) 
    @Html.DropDownListFor(x => x.SelectedHobbyId, Model.AvailableHobbies) 
    @Html.ValidationMessageFor(x => x.SelectedHobbyId) 

    <button type="submit">Create</button> 
}