填充mvc3中的下拉列表

时间:2012-02-27 12:35:07

标签: asp.net-mvc-3

我是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:没有为此对象定义无参数构造函数

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>
}