Return 几个复选框值 Html.CheckBoxFor 到控制器
Return several checkbox values of Html.CheckBoxFor to the controller
我有一个视图,用户应该 select 几个通过复选框列出的项目,然后应该在控制器中工作。
但是,我没有得到传递给控制器 POST 方法的值;调试时列表为空。
在搜索答案时,我找不到与 C# 和 Html.CheckboxFor
匹配的多个值。
我觉得这可能只是一个需要修复的小问题,但我已经好几天都弄不明白了。
这是我的模型:
public class ShownListofTools// This List is given to the View to contain all Tools
{
public IEnumerable<CheckboxItem> ToolsInShownList { get; set; } = new List<CheckboxItem>();
}
public class CheckboxItem //This Model is used to bring the Tools to the View
{
public int ShownToolID { get; set; }
public bool IsChecked { get; set; }
public string DisplayName { get; set; }
public int RemainingWorkTime { get; set; }
}
这是控制器:
[HttpGet]
public ActionResult IndexforLend()
{
// 1)
var notwornouttools = db.Tools.Where(t => t.Max_Worktime - t.CurrentWorktime > 0); // List Tools that are not worn out yet
var tools = notwornouttools.Where(t => t.UserID == null); //refine Filter to all tools that are currently not lent by other users
// 2)
List<CheckboxItem> SelectListTools = new List<CheckboxItem>(); //temporary list, to gather all needed items
// 3)
foreach(var item in tools)//work down the "tool" list from above
{
SelectListTools.Add(
new CheckboxItem()//Add a new Checkbox Item
{
DisplayName = item.Tooltype + " " + item.Diameter+"mm", //Transfer the Attributes from the "tool" list
ShownToolID = item.ID,
IsChecked = false,
RemainingWorkTime = item.Max_Worktime - item.CurrentWorktime,
}
);
}
//Checkboxlist.ToolsInShownList = SelectListTools; //The Checkbox Items are Added to the List "Checkboxlist".
return View(SelectListTools); // The List "Checkboxlist" is transferred to the View.
}
//POST: Add Tools to a User
[HttpPost]
public ActionResult IndexforLend([Bind(Include = "IsChecked,ShownToolID")] List<CheckboxItem> chklist) //import from View, but make it numerable
{
foreach(var item in chklist) //go through the entire Checkboxlist
{
if (item.IsChecked == true) // if the Checkbox was Checked - not sure if only checked objects are returned
{
var id = item.ShownToolID; // take the Tool-ID to the variable "id"
foreach(var realtool in db.Tools) //go through all Tools in Database
{
if (realtool.ID == id) // if the Id from the Checkbox is found
{
realtool.UserID = LoggedInUser.ID; // set the Foreign Key UserID to the ID of the Logged in User
db.Entry(realtool).State = EntityState.Modified;
db.SaveChanges();
}
}
};
}
return RedirectToAction("Index");
}
观点:
@model List<WZ_Web2.Models.CheckboxItem>
<p></p>
<p></p>
<div>Please select the Tools you want to lend:</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table class="table">
@**@
<tr>
<th>
Select
</th>
<th>
Tool Name
</th>
<th>
Remaining Work Time
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.CheckBoxFor(modelItem => item.IsChecked)
@Html.HiddenFor(modelItem => item.ShownToolID)
</td>
<td>
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RemainingWorkTime)
</td>
</tr>
}
</table>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="submit" class="btn btn-default" />
</div>
}
这样使用 foreach
它不会在 post 中生成索引名称列表。当您迭代 item
并设置 modelItem
时,它会在 post:
中生成类似这样的内容
item.IsChecked=true&item.ShownToolID=1&item.IsChecked=false&item.ShownToolID=2...
但控制器需要一个索引列表,例如:
[0].IsChecked=true&[0].ShownToolID=1&[1].IsChecked=false&[1].ShownToolID=2...
因此,mapper 无法绑定您 post 中的数据。我通过检查 post 弄明白了。尝试使用 for
代替...
在您看来:
@for (int i = 0; i < Model.Count; i++ )
{
<tr>
<td>
@Html.CheckBoxFor(item => Model[i].IsChecked)
@Html.HiddenFor(item => Model[i].ShownToolID)
</td>
<td>
@Html.DisplayFor(item => Model[i].DisplayName)
</td>
<td>
@Html.DisplayFor(item => Model[i].RemainingWorkTime)
</td>
</tr>
}
在你的控制器中:
[HttpPost]
public ActionResult IndexforLend(List<CheckboxItem> chklist)
我有一个视图,用户应该 select 几个通过复选框列出的项目,然后应该在控制器中工作。
但是,我没有得到传递给控制器 POST 方法的值;调试时列表为空。
在搜索答案时,我找不到与 C# 和 Html.CheckboxFor
匹配的多个值。
我觉得这可能只是一个需要修复的小问题,但我已经好几天都弄不明白了。
这是我的模型:
public class ShownListofTools// This List is given to the View to contain all Tools
{
public IEnumerable<CheckboxItem> ToolsInShownList { get; set; } = new List<CheckboxItem>();
}
public class CheckboxItem //This Model is used to bring the Tools to the View
{
public int ShownToolID { get; set; }
public bool IsChecked { get; set; }
public string DisplayName { get; set; }
public int RemainingWorkTime { get; set; }
}
这是控制器:
[HttpGet]
public ActionResult IndexforLend()
{
// 1)
var notwornouttools = db.Tools.Where(t => t.Max_Worktime - t.CurrentWorktime > 0); // List Tools that are not worn out yet
var tools = notwornouttools.Where(t => t.UserID == null); //refine Filter to all tools that are currently not lent by other users
// 2)
List<CheckboxItem> SelectListTools = new List<CheckboxItem>(); //temporary list, to gather all needed items
// 3)
foreach(var item in tools)//work down the "tool" list from above
{
SelectListTools.Add(
new CheckboxItem()//Add a new Checkbox Item
{
DisplayName = item.Tooltype + " " + item.Diameter+"mm", //Transfer the Attributes from the "tool" list
ShownToolID = item.ID,
IsChecked = false,
RemainingWorkTime = item.Max_Worktime - item.CurrentWorktime,
}
);
}
//Checkboxlist.ToolsInShownList = SelectListTools; //The Checkbox Items are Added to the List "Checkboxlist".
return View(SelectListTools); // The List "Checkboxlist" is transferred to the View.
}
//POST: Add Tools to a User
[HttpPost]
public ActionResult IndexforLend([Bind(Include = "IsChecked,ShownToolID")] List<CheckboxItem> chklist) //import from View, but make it numerable
{
foreach(var item in chklist) //go through the entire Checkboxlist
{
if (item.IsChecked == true) // if the Checkbox was Checked - not sure if only checked objects are returned
{
var id = item.ShownToolID; // take the Tool-ID to the variable "id"
foreach(var realtool in db.Tools) //go through all Tools in Database
{
if (realtool.ID == id) // if the Id from the Checkbox is found
{
realtool.UserID = LoggedInUser.ID; // set the Foreign Key UserID to the ID of the Logged in User
db.Entry(realtool).State = EntityState.Modified;
db.SaveChanges();
}
}
};
}
return RedirectToAction("Index");
}
观点:
@model List<WZ_Web2.Models.CheckboxItem>
<p></p>
<p></p>
<div>Please select the Tools you want to lend:</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table class="table">
@**@
<tr>
<th>
Select
</th>
<th>
Tool Name
</th>
<th>
Remaining Work Time
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.CheckBoxFor(modelItem => item.IsChecked)
@Html.HiddenFor(modelItem => item.ShownToolID)
</td>
<td>
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td>
@Html.DisplayFor(modelItem => item.RemainingWorkTime)
</td>
</tr>
}
</table>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="submit" class="btn btn-default" />
</div>
}
这样使用 foreach
它不会在 post 中生成索引名称列表。当您迭代 item
并设置 modelItem
时,它会在 post:
item.IsChecked=true&item.ShownToolID=1&item.IsChecked=false&item.ShownToolID=2...
但控制器需要一个索引列表,例如:
[0].IsChecked=true&[0].ShownToolID=1&[1].IsChecked=false&[1].ShownToolID=2...
因此,mapper 无法绑定您 post 中的数据。我通过检查 post 弄明白了。尝试使用 for
代替...
在您看来:
@for (int i = 0; i < Model.Count; i++ )
{
<tr>
<td>
@Html.CheckBoxFor(item => Model[i].IsChecked)
@Html.HiddenFor(item => Model[i].ShownToolID)
</td>
<td>
@Html.DisplayFor(item => Model[i].DisplayName)
</td>
<td>
@Html.DisplayFor(item => Model[i].RemainingWorkTime)
</td>
</tr>
}
在你的控制器中:
[HttpPost]
public ActionResult IndexforLend(List<CheckboxItem> chklist)