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
与所选属性绑定,但在单击CheckBox
时,它不会更新为true!如果只有一个CheckBox
,相同的方法可以正常工作。但是,当我在控制器中读取属性MarketingCodes
的值时,我正在使用循环它不起作用,所有选定的属性都是false。
我从UI获取值的控制器代码:
我编写了这个私有函数,当索引get方法触发时调用,当post方法触发 时,因为viewmodel
在发布表单时没有保留任何值。我想这是所有选定属性都设置为false的点。
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);
}
}
答案 0 :(得分:2)
您正在更改每个复选框的name
属性 - 这是默认模型绑定器尝试将数据绑定到模型时使用的属性,最好让MVC为您生成,例如。
@for (int i = 0; i < Model.MarketingCodes.Count(); i++)
{
...
Html.CheckBoxFor(model => model.MarketingCodes[i].selected);
...
}
答案 1 :(得分:0)
答案可以在问题本身中看到。覆盖复选框的名称不是问题,而是我想覆盖名称,以便标签的属性与复选框的名称一起使用。因此,如果用户单击标签,则会单击该复选框。 实际问题是我将数据绑定到MarketingCodes属性的方式。我已经给出了旧版本以及更新版本。它现在工作正常。