1

所以我有这个类可以让我检查是否选择了颜色:

public class Colors
{
    public bool mIsSelected { get; set; }
    public string mName { get; set; }
}

在将列表发送到视图之前,我使用以下值填充它:

public List<Colors> mListColors = new List<Colors>();

mListColors.Add(new Colors{ mIsSelected = false, mName = "black"});
mListColors.Add(new Colors { mIsSelected = false, mName = "blue" });
mListColors.Add(new Colors { mIsSelected = false, mName = "white" });
mListColors.Add(new Colors { mIsSelected = false, mName = "red" });
mListColors.Add(new Colors { mIsSelected = false, mName = "green" });

在我看来,我将它们渲染成这样:

@for (int i = 0; i < Model.mListColors.Count; i++)
{
    var item = Model.mListColors[i];

switch (item.mName)
{
    case "black":
        @Html.Image("~\\Images\\Functional\\blackColor.jpeg", item.mName.ToUpper(), null)
        @Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
        @Html.HiddenFor(_item => _item.mListColors[i].mName)
        break;
    case "blue":
        @Html.Image("~\\Images\\Functional\\blueColor.jpeg", item.mName.ToUpper(), null)
        @Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
        @Html.HiddenFor(_item => _item.mListColors[i].mName)
        break;
    case "red":
        @Html.Image("~\\Images\\Functional\\redColor.jpeg", item.mName.ToUpper(), null)
        @Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
        @Html.HiddenFor(_item => _item.mListColors[i].mName)
        break;
    case "green":
        @Html.Image("~\\Images\\Functional\\greenColor.jpeg", item.mName.ToUpper(), null)
        @Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
        @Html.HiddenFor(_item => _item.mListColors[i].mName)
        break;
    case "white":
        @Html.Image("~\\Images\\Functional\\whiteColor.jpeg", item.mName.ToUpper(), null)
        @Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
        @Html.HiddenFor(_item => _item.mListColors[i].mName)
        break;
}

}

这样我可能有一个复选框并将值设置为 true 或 false 取决于是否选中了复选框。HtmlImage 是一个自定义助手,允许我显示图像。

但是无论是否选中该框,控制器中的值始终为假,那么我该怎么做才能更正呢?

4

3 回答 3

0

根据 Jason Evans 的建议,您还可以重构代码,以免违反 DRY

@for(int idx = 0;idx < Model.ListColors.Count;idx++)
{
    var item = Model.ListColors[idx]
    @Html.Image(string.Format(
           "~\\Images\\Functional\\{0}Color.jpeg",
           item.Name.ToLower()), item.Name, null)
    @Html.CheckBoxFor(model => Model[idx].IsSelected, item.IsSelected)
    @Html.HiddenFor(model => Model[idk].Name)
}

更新

您可能需要考虑创建一个更准确地反映您的数据的模型:

public class Colors
{
    public bool IsSelected {get;set;}
    public string Name {get;set;}
}

并将您的模型公开为:

@model IEnumerable<Colors>
于 2013-06-25T15:48:24.390 回答
0

您的语法有点不对劲,您将它们全部设置为 false。看这里

// Assign the result of a boolean expression to b.
b = (days % 2 == 0);
于 2013-06-25T15:42:51.210 回答
0

如果我又错了,请投反对票,但不应该是Value吗?

@Html.CheckBoxFor(_item => color.Value, Model.mListColors["Blue"].Value)

于 2013-06-25T16:00:16.607 回答