0

具有以下ViewModel代码:

public List<MarketingCode> MarketingCodes { get; set; }

哪里MarketingCode是类型:

public class MarketingCode
{

    public bool selected
    {
        get;
        set;
    }
    public MarketingOptionCode name { get; set; }
    public string Line { get; set; }
    public string value { get; set; }

    [Flags]//Indicates that an enumeration can be treated as a bit field; i.e., as a set of flags
    public enum MarketingOptionCode
    {

        Email = 1,

        Post = 2,

        SMS = 3,

        Telephone = 4
    }

}

我的视图有以下代码来呈现CheckBoxList

 @for (int i = 0; i < Model.MarketingCodes.Count(); i++)
                                    {
                                        var name = @Model.MarketingCodes[i].name.ToString();
                                        <label for="@Model.MarketingCodes[i].name" class="checkbox inlineblock">
                                            @Html.CheckBoxFor(model => model.MarketingCodes[i].selected, new { name = @Model.MarketingCodes[i].name.ToString(), @id = @Model.MarketingCodes[i].name.ToString() }) **@* This works totally fine, even if you click the label, the checkbox will be checked. *@**

                                            @Model.MarketingCodes[i].name
                                        </label>
                                    }

如您所见,CheckBoxes它们与选定的属性绑定,但单击时它不会更新为 true CheckBox!如果只有一个CheckBox. 但是,当我在控制器中读取属性的值时,我正在使用一个循环,它不起作用MarketingCodes,所有选定的属性都是错误的。

我从 UI 获取值的控制器代码:

我已经编写了这个私有函数,以便在索引 get 方法触发以及 post 方法触发时调用,因为在viewmodel发布表单时它没有保存任何值。我猜这是所有选定属性都设置为假的地方。

 private static void BindMarketingOptions(PersonalDetailsIndexViewModel viewModel)
    {
        viewModel.MarketingCodes = new[] { 

            new Bike.ViewModels.MarketingCode { name = Bike.ViewModels.MarketingCode.MarketingOptionCode.Email, Line= "line1", value="value1" },
            new Bike.ViewModels.MarketingCode { name = Bike.ViewModels.MarketingCode.MarketingOptionCode.Post, Line= "line2", value="value2"  },
                new Bike.ViewModels.MarketingCode { name = Bike.ViewModels.MarketingCode.MarketingOptionCode.SMS,Line= "line3", value="value3"  },
                    new Bike.ViewModels.MarketingCode { name = Bike.ViewModels.MarketingCode.MarketingOptionCode.Telephone, Line= "line4", value="value4"  }
                    }.ToList();
    }

我已将上述方法更改为以下方法,以使我选择的属性起作用:

private static void BindMarketingOptions(PersonalDetailsIndexViewModel viewModel)
    {
        if (viewModel.MarketingCodes == null)
        {
            viewModel.MarketingCodes = new[] { 

            new Bike.ViewModels.MarketingCode { name = Bike.ViewModels.MarketingCode.MarketingOptionCode.Email, Line= "line1", value="value1"},
            new Bike.ViewModels.MarketingCode { name = Bike.ViewModels.MarketingCode.MarketingOptionCode.Post, Line= "line2", value="value2"},
                new Bike.ViewModels.MarketingCode { name = AQuoteBike.ViewModels.MarketingCode.MarketingOptionCode.SMS,Line= "line3", value="value3"},
                    new Bike.ViewModels.MarketingCode { name = Bike.ViewModels.MarketingCode.MarketingOptionCode.Telephone, Line= "line4", value="value4"}
                    }.ToList();
        }
        for (var i = 0; i < viewModel.MarketingCodes.Count(); i++)
        {
            viewModel.MarketingCodes[i].selected = viewModel.MarketingCodes[i].selected;
        }


    }
BindMarketingOptions(viewModel);
for (var i = 0; i <= viewModel.MarketingCodes.Count(); i++)
                {
                    UserField userField = new UserField();
                    if (viewModel.MarketingCodes[i].selected)  //<--FALSE FOR ALL THE CHECKBOXES
                    {
                        userField.Line = viewModel.MarketingCodes[i].Line;
                        userField.Type = viewModel.MarketingCodes[i].name.ToString();
                        userField.Value = viewModel.MarketingCodes[i].value;  
                        BikeQuote.BikeRisk.UserFields.Add(userField);
                    }
                }
4

2 回答 2

2

您正在更改name每个复选框的属性 - 这是默认模型绑定器用来尝试将数据绑定到模型的属性,最好让 MVC 为您生成它,例如

@for (int i = 0; i < Model.MarketingCodes.Count(); i++)
{
    ...
    Html.CheckBoxFor(model => model.MarketingCodes[i].selected);
    ...
}
于 2013-01-12T11:19:38.703 回答
0

答案可以从问题本身中看出。覆盖复选框的名称不是问题,而是我想覆盖名称,以便标签的属性与复选框的名称一起使用。因此,如果用户单击标签,则会单击复选框。实际问题是我将数据绑定到 MarketingCodes 属性的方式。我已经给出了旧版本以及更新版本。现在它工作正常。

于 2013-01-12T13:29:13.447 回答