英文:
How to get list of checkboxes not checked on page model razor page?
问题
I working on razor page with .NET core 7. I face issue I can't pass checkboxes that are not checked with values as 1, 2, 3, from the HTML page to the page model on the post method? So, when the user clicks the submit button, the post method should collect the values of the unchecked checkboxes as a list. Here's the view model used:
public class UserAccessViewModel
{
public UserAccess userAccess { get; set; }
public int[] checks { get; set; }
}
In the page.cshtml
file, you have checkboxes defined like this:
<input id="StockTake" name="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="1">
<label for="lblStockTake">Stock Take</label>
<input id="ShelfLabelPrint" name="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="2">
<label for="lblShelfLabel">Shelf Label Print</label>
<input id="Transfer" name="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="3">
<label for="lblTransfer" style="margin-right:5px;">Transfer</label>
In the pagemodel.cshtml.cs
file, you can receive the unchecked checkboxes in the OnPost
method of your page model like this:
public async Task OnPost(UserAccessViewModel UserAccessViewModel)
{
// You can access the unchecked checkboxes in UserAccessViewModel.checks here.
}
For more details, please refer to the image: pass unchecked checkboxes to razor page.
英文:
I working on razor page with .NET core 7 . I face issue I can't pass checkboxes that not checked with values as 1 2 3 from html page to page model on post method ?
so user click submit button then on post method fire then it will collect check boxes values not checked as list
view model used
public class UserAccessViewModel
{
public UserAccess userAccess { get; set; }
public int[] checks { get; set; }
}
on page.cshtml
@Html.AntiForgeryToken()
<form method="post">
<button id="FillCheckBox" type="submit" class="col-sm-1 btn btn-primary">Submit</button>
<input id="StockTake" name="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="1">
<label for="lblStockTake">Stock Take</label>
<input id="ShelfLabelPrint" name="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="2">
<label for="lblShelfLabel">Shelf Label Print</label>
<input id="Transfer" name="UserAccessViewModel.checks" type="checkbox" asp-for="UserAccess.MODULECODE" value="3">
<label for="lblTransfer" style="margin-right:5px;">Transfer</label>
</form>
on pagemodel .cshtml.cs
public async Task OnPost(UserAccessViewModel UserAccessViewModel)
{
//How to pass checkboxes not checked to razor page
}
for more details what I need
答案1
得分: 1
以下是您要翻译的内容:
尝试修改您的视图模型:
public class UserAccessViewModel
{
//其他属性...
public List<CheckBoxModel> Checks { get; set; } = default!;
}
public class CheckBoxModel
{
public int Value { get; set; }
public bool Selected { get; set; }
public string Label { get; set; } = default!;
}
页面模型(我尝试使用默认的RazorPage项目的Privacy Page):
public class PrivacyModel : PageModel
{
//.......
[BindProperty(SupportsGet = true)]
public UserAccessViewModel userAccessViewModel { get; set; }
public void OnGet()
{
userAccessViewModel = new UserAccessViewModel()
{
Checks = new List<CheckBoxModel>
{
new CheckBoxModel(){Value=1,Label="Stock Take"} ,
new CheckBoxModel(){Value=2,Label="Shelf Label Print"} ,
new CheckBoxModel(){Value=3,Label="Transfer"} ,
}
};
}
public void OnPost(UserAccessViewModel UserAccessViewModel)
{
}
}
页面中的代码(添加隐藏输入来保存整数值,并使用`asp-for`来绑定复选框的选中状态):
@page
@model PrivacyModel
<form method="post">
<button id="FillCheckBox" type="submit" class="col-sm-1 btn btn-primary">Submit</button>
@for(int i = 0; i < Model.userAccessViewModel.Checks.Count; i++)
{
<input asp-for="userAccessViewModel.Checks[i].Selected" type="checkbox"/>
<input asp-for="userAccessViewModel.Checks[i].Value" hidden />
<label asp-for="userAccessViewModel.Checks[i].Label">@Model.userAccessViewModel.Checks[i].Label</label>
<input asp-for="userAccessViewModel.Checks[i].Label" hidden />
}
</form>
结果:
[![enter image description here][1]][1]
未选中的复选框:
var notchecked = userAccessViewModel.Checks.Where(x => x.Selected == false).ToList();
希望这有所帮助。如果需要进一步的帮助,请告诉我。
英文:
try modify your viewmodel:
public class UserAccessViewModel
{
//other properties...
public List<CheckBoxModel> Checks { get; set; } = default!;
}
public class CheckBoxModel
{
public int Value { get; set; }
public bool Selected { get; set; }
public string Label { get; set; } = default!;
}
Page Model(I tried with Privacy Page of a default RazorPage project):
public class PrivacyModel : PageModel
{
//.......
[BindProperty(SupportsGet =true)]
public UserAccessViewModel userAccessViewModel { get; set; }
public void OnGet()
{
userAccessViewModel=new UserAccessViewModel()
{
Checks=new List<CheckBoxModel>
{
new CheckBoxModel(){Value=1,Label="Stock Take"} ,
new CheckBoxModel(){Value=2,Label="Shelf Label Print"} ,
new CheckBoxModel(){Value=3,Label="Transfer"} ,
}
};
}
public void OnPost(UserAccessViewModel UserAccessViewModel)
{
}
}
Codes in Page(add hidden input hold your int values and bind the statue of selected or not with asp-for
in checkbox):
@page
@model PrivacyModel
<form method="post">
<button id="FillCheckBox" type="submit" class="col-sm-1 btn btn-primary">Submit</button>
@for(int i = 0; i < Model.userAccessViewModel.Checks.Count; i++)
{
<input asp-for="userAccessViewModel.Checks[i].Selected" type="checkbox"/>
<input asp-for="userAccessViewModel.Checks[i].Value" hidden />
<label asp-for="userAccessViewModel.Checks[i].Label">@Model.userAccessViewModel.Checks[i].Label</label>
<input asp-for="userAccessViewModel.Checks[i].Label" hidden />
}
</form>
Result:
not selected checkbox:
var notchecked = userAccessViewModel.Checks.Where(x => x.Selected == false).ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论