在MVC 6中,如何在视图中复选框列表并将检查的值传递给控制器? [英] In MVC 6, how to code checkbox list in view and pass the checked values to the controller?
问题描述
很抱歉,我的大多数搜索都引用了旧的MVC代码。
Sorry but most of my searches take me to old MVC codes. Any help will be appreciated.
在MVC 6中使用标记助手,如何对一组复选框进行编码:
In MVC 6 with tag helpers, how do you code a set of checkboxes:
- 对标签使用标记助手
- 将选中的值保存(绑定?)到IsOptionSelected属性
- 点击提交后将这些选中的值传回到控制器
?
我能够正确显示带有标签的复选框,但我不知道如何通过模型将检查的值传递回控制器。现在,IsOptionSelected的值返回为假。
I was able to display the checkboxes with labels correctly, but I do not know how to pass the checked values back to the controller via the model. Right now, IsOptionSelected values are coming back as false.
我也能够使标签的html助手工作,但不是标签助手。我可能也编码这些都错了,所以任何提示都会帮助!
I was also able to make the html helper for the label work but not for the tag helper. I may be also coding these all wrong so any tips will help!
这里是我到目前为止:
显示:
实体:
public class PhoneOption
{
public bool IsOptionSelected { get; set; } = false;
public int OptionId { get; set; }
public string OptionName { get; set; }
}
型号:
$ b
Model:
[Display(Name = "Phone Options")]
public IEnumerable<PhoneOption> PhoneOptions { get; set; }
. . . .
PhoneOptions = repository.GetPhoneOptions();
存储库:
public IEnumerable<PhoneOption> GetPhoneOptions()
{
IEnumerable<PhoneOption> options = new[]
{
new PhoneOption { OptionId = 1, OptionName = "Phone Case", IsOptionSelected = false },
new PhoneOption { OptionId = 2, OptionName = "Screen Protector", IsOptionSelected = false },
new PhoneOption { OptionId = 3, OptionName = "Car Charger", IsOptionSelected = false },
new PhoneOption { OptionId = 4, OptionName = "Extra Cable", IsOptionSelected = false }
};
return options;
}
查看: b
<div class="form-group">
<label class="control-label">Phone Options</label>
<div>
@foreach (var option in Model.PhoneOptions)
{
<div>
@{ string cbId = "PhoneOption_" + @option.OptionId; }
<input asp-for=@option.IsOptionSelected type="checkbox" value=@option.IsOptionSelected id=@cbId name=@cbId />
@Html.Label(@cbId.ToString(), @option.OptionName)
@*This is causing invalid operation exception*@
@*<label asp-for=@cbId.ToString()>@option.OptionName</label>*@
<span asp-validation-for=@cbId class="text-danger" role="alert"></span>
</div>
}
</div>
</div>
提前感谢!
推荐答案
这最后是我做了,使它的工作。我不知道这是否是最好的方法。我必须仍然使用html助手,因为标记助手不起作用。
This is finally what I did to make it to work. I am not sure if this is the best way to do it. I had to still use the html helpers because the tag helpers do not work.
模型:
public List<PhoneOption> PhoneOptions { get; set; }
. . .
PhoneOptions = repository.GetPhoneOptions().ToList();
查看:
@if (@Model.PhoneOptions != null && @Model.PhoneOptions.Count() > 0)
{
for (int i = 0; i < @Model.PhoneOptions.Count(); i++)
{
<div>
<input asp-for="@Model.PhoneOptions[i].IsOptionSelected" type="checkbox" />
<label asp-for="@Model.PhoneOptions[i].IsOptionSelected">@Model.PhoneOptions[i].OptionName</label>
@*If these are not included, all OptionIds become 0 and all OptionName becomes null*@
@Html.HiddenFor(x => @Model.PhoneOptions[i].OptionId)
@Html.HiddenFor(y => @Model.PhoneOptions[i].OptionName)
</div>
}
}
我希望这可以帮助别人复选框列表问题。
I hope this helps someone else who is having the same checkbox list issues.
UPDATE:
我已将html助手更新为上述标记助手。
UPDATE: I've updated the html helpers to tag helpers above.
这篇关于在MVC 6中,如何在视图中复选框列表并将检查的值传递给控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!