大佬教程收集整理的这篇文章主要介绍了ASP.NET Core 3.1 MVC:将选定的复选框发送到控制器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下带有复选框的列表视图:
@model IEnumerable<PaketserviSaracTakip.Models.Item>
@{
VIEwData["title"] = "Yükleme Yap";
Layout = "~/VIEws/Shared/_Layout.csHTML";
}
<h2 class="text-center text-success">@VIEwBag.name İsimli araca Yükleme Yap</h2>
<form asp-controller="Vehicle"
asp-action="LoadItem" method="post">
<br />
<input type="submit" name="submit" value="oluştur" class="btn btn-priMary" />
</form>
<table class="table table-bordered table-Striped table-hover">
<thead>
<tr>
<th class="text-center">
Yüklensin mi?
</th>
<th class="text-center">
@HTMl.displaynameFor(model => model.Name)
</th>
<th class="text-center">
@HTMl.displaynameFor(model => model.PricE)
</th>
<th class="text-center">
@HTMl.displaynameFor(model => model.description)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
<div class="form-check">
<label class="form-check-label" for="@item.ID" asp-valIDation-for="@item.ID"></label>
<input class="form-check-input" name="IDs" type="checkBox" value="@item.ID" ID="@item.ID">
</div>
</td>
<td>
@HTMl.displayFor(modelitem => item.Name)
</td>
<td>
₺@HTMl.displayFor(modelitem => item.PricE)
</td>
<td>
@HTMl.displayFor(modelitem => item.Description)
</td>
</tr>
}
</tbody>
</table>
我的项目模型与数据库集成:
[table("Items")]
public class Item
{
[Key]
public int id { get; set; }
[display(name = "İsim")]
[required(Errormessage ="{0} alanı boş bırakılamaz.")]
[MaxLength(50,Errormessage ="İsim 50 karakteri geçemez.")]
public String name { get; set; }
[display(name = "Fiyat")]
[required(Errormessage = "{0} alanı boş bırakılamaz.")]
[Range(0,Double.MaxValue,Errormessage = "Minimum 0 girmelisin.")]
public int Price { get; set; }
[display(name = "Açıklama")]
public String Description { get; set; }
}
view
所以当按钮被点击时,我想在我的控制器中获得选中的项目。我试过了,但它是空的:
[httpPost]
public ActionResult LoadItem(IEnumerable<Item> model)
{
return RedirectToAction("Index");
}
我也尝试过 int array 和 FormCollection 但没有奏效。我想我需要一些标签助手,但不知道是哪个。
当单击按钮时,我想在我的控制器中获得选中的项目。一世 试过这个,但它是空的
请检查查看页面中的代码,由于表格不在<form>
元素中,当您点击Submit
按钮时,提交的表单不包含相关数据。
此外,要使用模型绑定将模型数据提交给控制器,我们应该使用 @for
语句遍历实体并使用隐藏字段来存储相关数据。请参考以下示例并更改您的代码:
型号:
[Table("Items")]
public class Item
{
[Key]
public int id { get; set; }
[Display(Name = "İsim")]
[required(Errormessage = "{0} alanı boş bırakılamaz.")]
[MaxLength(50,Errormessage = "İsim 50 karakteri geçemez.")]
public String Name { get; set; }
[Display(Name = "Fiyat")]
[required(Errormessage = "{0} alanı boş bırakılamaz.")]
[Range(0,Double.MaxValue,Errormessage = "Minimum 0 girmelisin.")]
public int Price { get; set; }
[Display(Name = "Açıklama")]
public String Description { get; set; }
public Boolean Ischecked { get; set; } //add a property to store whether the item is check or not.
}
查看页面:
@model List<netcore3_1sample.Models.Item>
@{
ViewData["title"] = "ItemIndex";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2 class="text-center text-success">@ViewBag.name İsimli Araca Yükleme Yap</h2>
<form asp-controller="Home"
asp-action="LoadItem" method="post">
<br />
<input type="submit" name="submit" value="Oluştur" class="btn btn-priMary" />
<br />
<table class="table table-bordered table-Striped table-hover">
<thead>
<tr>
<th class="text-center">
Yüklensin mi?
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
Price
</th>
<th class="text-center">
Description
</th>
</tr>
</thead>
<tbody>
@for( var i = 0; i < Model.Count;i++)
{
<tr>
<td>
<div class="form-check">
<label class="form-check-label" for="@model[i].Id" asp-validation-for="@model[i].Id"></label>
<input class="form-check-input" name="ids" type="checkbox" value="@model[i].Id" id="@model[i].Id">
@*<input type="checkbox" asp-for="@model[i].Ischecked" />*@
<input type="hidden" asp-for="@model[i].Id" />
</div>
</td>
<td>
@Html.DisplayFor(modelItem => Model[i].Name)
<input type="hidden" asp-for="@model[i].Name" />
</td>
<td>
₺@Html.DisplayFor(modelItem => Model[i].PricE)
<input type="hidden" asp-for="@model[i].Price" />
</td>
<td>
@Html.DisplayFor(modelItem => Model[i].Description)
<input type="hidden" asp-for="@model[i].Description" />
</td>
</tr>
}
</tbody>
</table>
</form>
控制器中的代码:
[httpPost]
public ActionResult LoadItem(List<Item> model,int[] ids)
{
return RedirectToAction("ItemIndex");
}
根据你的代码,你使用的是一个html checkbox元素来存储选中的Item ID,所以这里我们需要添加一个数组来获取选中的id。
此外,您可以在 Item 模型中添加一个 Ischecked
属性,然后更改以下代码:
<input class="form-check-input" name="ids" type="checkbox" value="@model[i].Id" id="@model[i].Id">
到
<input type="checkbox" asp-for="@model[i].Ischecked" />
通过使用此方法,您可以在控制器中根据 Ischecked 属性过滤所选项目。
结果如下:
以上是大佬教程为你收集整理的ASP.NET Core 3.1 MVC:将选定的复选框发送到控制器全部内容,希望文章能够帮你解决ASP.NET Core 3.1 MVC:将选定的复选框发送到控制器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。