复选框不会将mvc应用程序中的bool属性从false更新为true

本文关键字:属性 false 更新 true bool mvc 应用程序 复选框 | 更新日期: 2023-09-27 17:51:24

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获取值的控制器代码:

我已经编写了这个私有函数,当索引获取方法触发时调用,当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);
                    }
                }

复选框不会将mvc应用程序中的bool属性从false更新为true

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

@for (int i = 0; i < Model.MarketingCodes.Count(); i++)
{
    ...
    Html.CheckBoxFor(model => model.MarketingCodes[i].selected);
    ...
}

答案可以从问题本身看出。重写复选框的名称不是问题,相反,我想重写名称,以便标签的属性与复选框的名称一起工作。因此,如果用户单击标签,则单击复选框。实际的问题是我将数据绑定到MarketingCodes属性的方式。我提供了旧版本和更新版本。

相关文章: