asp.net-mvc 使用模型绑定从 MVC 4 中的 List<T> 中选择项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10984727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:00:59  来源:igfitidea点击:

Select Items from List<T> in MVC 4 using Model Binding

asp.net-mvcasp.net-mvc-4model-binding

提问by Eric J.

Given a class

给定一个班级

public class Person
{
    // Some general properties

    public List<Hobby> Hobbies { get; set; }
}

public class Hobby
{
    // Some properties e.g. Name, etc.
}

static List<Hobby> AllHobbies { get; }

Is it possible to create a view that allows the user to select his hobbies using model binding?

是否可以创建一个允许用户使用模型绑定选择他的爱好的视图?

It would certainly be possible in the view to loop through AllHobbiesand render an <input type="checkbox" />for each, then wire up the selected values by hand in the postback controller. It seems that this should be doable with model binding, but I don't see how.

在视图中当然可以循环AllHobbies<input type="checkbox" />为每个渲染一个,然后在回发控制器中手动连接选定的值。似乎这应该可以通过模型绑定来实现,但我不知道怎么做。

回答by Darin Dimitrov

Sure, I would recommend you using editor templates.

当然,我建议您使用编辑器模板。

Let's suppose that a hobby has a name and a boolean field indicating whether it was selected by the user:

让我们假设一个爱好有一个名称和一个布尔字段,指示它是否被用户选择:

public class Hobby
{
    public string Name { get; set; }
    public bool Selected { get; set; }
}

then a controller to feed the model into the view and process the form submission:

然后是一个控制器,将模型输入视图并处理表单提交:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var person = new Person
        {
            Hobbies = new[]
            {
                new Hobby { Name = "hobby 1" },
                new Hobby { Name = "hobby 2", Selected = true },
                new Hobby { Name = "hobby 3" },
            }.ToList()
        };
        return View(person);
    }

    [HttpPost]
    public ActionResult Index(Person person)
    {
        var selectedHobbies = person
            .Hobbies
            .Where(x => x.Selected).Select(x => x.Name);
        string message = string.Join(",", selectedHobbies);
        return Content("Thank you for selecting: " + message);
    }
}

then a view containing the form allowing the user to select hobbies:

然后是一个包含允许用户选择爱好的表单的视图:

@model Person

@using (Html.BeginForm()) 
{
    <h2>Hobbies</h2>
    @Html.EditorFor(x => x.Hobbies)
    <button type="submit">OK</button>
}

and a corresponding editor template which will automatically be rendered for each element of the Hobbiescollection (~/Views/Home/EditorTemplates/Hobby.cshtml-> notice that the name and location of the template is important):

以及一个相应的编辑器模板,它将为Hobbies集合的每个元素自动呈现(~/Views/Home/EditorTemplates/Hobby.cshtml-> 注意模板的名称和位置很重要):

@model Hobby

<div>
    @Html.LabelFor(x => x.Selected, Model.Name)
    @Html.HiddenFor(x => x.Name)
    @Html.CheckBoxFor(x => x.Selected)
</div>

For more advanced editing scenarios I would recommend you going through the Steven Sanderson's blog poston this topic.

对于更高级的编辑场景,我建议您阅读 Steven Sanderson关于此主题的博客文章