具有以下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);
}
}