绑定@foreach中的每个DropDownListFor [英] Binding each DropDownListFor within @foreach

查看:58
本文介绍了绑定@foreach中的每个DropDownListFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的ASP.NET WebForms经验,这是我的第一个MVC项目.
这是我认为该机制所不允许的问题,但我可能是错的.
@foreach 中包含 DropDownListFor 并不是一件容易的事.
首先,下拉菜单不会选择我传递给它的值( m =>问题.答案)-在下面的图像中请注意,它们都显示 No Answer ,因为它没有选择我加载时所用的正确值.
其次,由于它们都以相同的表单域名称结尾,因此控制器如何将它们分隔回相应的List<>?
我已经看了一个小时,并搜索了类似的示例,但找不到任何示例.
什么不是硬编码的骇客或解决方法的好解决方案?

With my ASP.NET WebForms experience, this is my first MVC project.
Here's a problem that I don't think the mechanism allows, but I could be wrong.
Having a DropDownListFor within a @foreach is not that trivial.
First of all, the dropdown doesn't select the value I pass to it (m => question.Answer) -- notice in the image below they all show No Answer, because it didn't select the correct value I loaded it with.
Secondly, since they all end up with the same form-field name, how could the controller separate them back into the corresponding List<>?
I've been looking at this for an hour and searched for a similar example, but I can't find any.
What's a good solution that's not a hard-coded hack or a workaround?

型号:

public List<SelectListItem> PossibleAnswers
{
    get
    {
    return new List<SelectListItem>() 
    { 
        new SelectListItem() { Text = "No Answer", Value = "0" }, 
        new SelectListItem() { Text = "Very Bad", Value = "1" }, 
        new SelectListItem() { Text = "Bad", Value = "2" }, 
        new SelectListItem() { Text = "Average", Value = "3" }, 
        new SelectListItem() { Text = "Good", Value = "4" },
        new SelectListItem() { Text = "Very Good", Value = "5" } 
    };
    }
}
public enum SurveyAnswer
{
    NoAnswer,
    VeryBad,
    Bad,
    Average,
    Good,
    VeryGood
}
public class SurveyQuestionClass
{
    public int ID { get; set; }
    public string Question { get; set; }
    public SurveyAnswer Answer { get; set; }
}
public List<SurveyQuestionClass> QuestionsAnswers { get; set; }

查看:

@foreach (var question in Model.QuestionsAnswers)
{
    <tr>
        <td>
            @Html.DropDownListFor(m => question.Answer, Model.PossibleAnswers, new { style = "font-size: small;" })
        </td>
        <td>
            @Html.Raw(question.Question)
        </td>
    </tr>
}

它看起来像这样:

推荐答案

您应使用editortemplates

You should use editortemplates

这可以应用如下:

将可能的答案移至SurveyQuestionClass.那么您可以指定一个editortemplate像这样:

Move your possible answers to the SurveyQuestionClass. then you can specify an editortemplate like this:

@model SurveyQuestionClass

<tr>
    <td>
        @Html.DropDownListFor(m => m.Answer, Model.PossibleAnswers, new { style = "font-size: small;" })
    </td>
    <td>
        @Html.Raw(Model.Question)
    </td>
</tr>

Editortemplates是局部视图,应放置在views文件夹的EditorTemplates文件夹中.因此,如果这适用于您的主视图,则模板应位于:视图">主页">"EditorTemplates".

Editortemplates are partialviews which should be placed in an EditorTemplates folder in your views folder. So if this applies to your home view, the template should be located in: views > Home > EditorTemplates.

编辑器模板应命名为type.cshtml.所以在您的情况下:SurveyQuestionClass.cshtml

Editortemplates should be named as type.cshtml. so in your case: SurveyQuestionClass.cshtml

在您看来,结果很简单:

in your view, this results in simply:

@Html.EditorFor(m => m.QuestionsAnswers)

当您将模型恢复为控制器功能时,所有问题和答案都将绑定到QuestionsAnswers列表.遍历列表并阅读SurveyQuestionClass.Answer属性,将为您提供问题答案

When you get your model back in the controller function, all questions and answers are being bound to the QuestionsAnswers List. Iterating through the list and reading the SurveyQuestionClass.Answer property, will give you the question answer

这篇关于绑定@foreach中的每个DropDownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆