aar*_*len 0 vb.net asp.net checkbox asp.net-mvc asp.net-mvc-3
如果项目的复选框被选中,我正在尝试使用预填充列表的复选框来执行操作。
在我的模型中,我有一个列表,它使用 GET 函数填充除 AddThis 布尔值之外的每个属性:
Public Property TaskList As List(Of TaskItem)
Get
Return (From a In db.Web.Backlogs
Join b In db.Web.References On a.StatusID Equals b.RefID
Where a.RelateSprint Is Nothing And b.Name <> "Resolved" And b.Name <> "Closed"
Select New TaskItem With {
.ItemID = a.ItemID,
.ItemName = a.ItemName,
.RelateItem = If(a.RelateItem IsNot Nothing, a.RelateItem, 0),
.OwnerID = If(a.UserIDOwn IsNot Nothing, a.UserIDOwn, 0),
.TypeID = If(a.TypeID IsNot Nothing, a.TypeID, 0),
.Type = If(db.Web.Backlogs.Count(Function(t) t.RelateItem = a.ItemID) > 0, "Epic", If(a.TypeID <> db.Web.References.FirstOrDefault(Function(t) t.Name = "Bug" And t.RefTypeID = 14).RefID, "User Story", "Bug")),
.Priority = If(a.Priority = 1, "Low", If(a.Priority = 2, "Medium", If(a.Priority = 3, "High", If(a.Priority = 4, "Critical", String.Empty)))),
.KanBan = a.KanBan,
.WorkUnit = a.WorkUnit}).ToList
End Get
Set(value As List(Of TaskItem))
End Set
End Property
Public Class TaskItem
Public Property AddThis As Boolean
Public Property ItemID As Integer
Public Property ItemName As String
Public Property RelateItem As Integer
Public Property OwnerID As Integer
Public Property TypeID As Integer
Public Property Type As String
Public Property Priority As String
Public Property KanBan As Integer
Public Property WorkUnit As Integer
End Class
Run Code Online (Sandbox Code Playgroud)
在我看来,我使用 AddThis 布尔值为列表中的每个项目添加了一个复选框 @Html.CheckBoxFor(Function(m) i.AddThis, New With {.class = "ttip", .title = "Add This to the Sprint."})
然后在我的帖子控制器中,我有这个:
For Each i In model.TaskList
If i.AddThis = True Then
Dim Backlog As New Backlog
Backlog = db.Web.Backlogs.Find(i.ItemID)
Backlog.RelateSprint = SprintID
Backlog.KanBan = 1
Backlog.DateDue = Sprint.DateDue
Backlog.DateUpdated = Now
Try
db.Web.Entry(Backlog).State = EntityState.Modified
db.Web.SaveChanges()
Allgood = True
Catch ex As Exception
ModelState.AddModelError("", "Unable to save Sprint. Please Try Again")
Allgood = False
End Try
End If
Next
Run Code Online (Sandbox Code Playgroud)
我也试过 For Each i In model.TaskList.Where(Function(m) m.AddThis = True)
但是当我选中复选框并单击提交时,它不承认任何 AddThis 布尔值是真的。谁能让我知道我如何正确地做到这一点?
小智 5
首先,您尚未发布本场景中使用的整个视图和控制器操作方法。所以我在这里做一些假设。
正如@Alan 所说,这似乎是如何生成控件名称的问题。
这只是我快速创建的一个示例。
以下是一个与您创建的模型类似的简单模型。
public class ModelToSubmit
{
public List<TaskItem> TaskList { get; set; }
}
public class TaskItem
{
public bool Addthis { get; set; }
public string SomeProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以下是控制器操作方法的外观。
[HttpPost]
public ActionResult Index1(ModelToSubmit taskItems)
{
foreach (var item in taskItems.Tasks)
{
if (item.Addthis)
{
}
}
return Json("Index Success", JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult Index()
{
var tasks = new List<TaskItem>
{
new TaskItem{ SomeProperty = "Line1"},
new TaskItem{ SomeProperty = "Line2"},
new TaskItem{ SomeProperty = "Line3"},
new TaskItem{ SomeProperty = "Line4"},
new TaskItem{ SomeProperty = "Line5"},
};
var mod = new ModelToSubmit();
mod.TaskList = tasks;
return View(mod);
}
Run Code Online (Sandbox Code Playgroud)
以下是观点。
@model MVC3Stack.Models.ModelToSubmit
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm("Index1", "Home", FormMethod.Post))
{
<table>
@for (int i = 0; i < Model.TaskList.Count; i++)
{
<tr>
<td>
@Html.TextBoxFor(m => Model.TaskList[i].SomeProperty)
</td>
<td>
@Html.CheckBoxFor(m => Model.TaskList[i].Addthis)
</td>
</tr>
}
</table>
<input type="submit" value="Post" />
}
Run Code Online (Sandbox Code Playgroud)
使用此模型并查看表格中的文本框和复选框列表,如下面的快照所示。

当我点击 post 按钮时,整个TaskList模型都充满了 taskItems
这是我选择前两个项目并发布时的快速观看窗口的快照。代码在 c# 中,但您可以轻松地将其转换为 vb.net。

希望这对你有用。
| 归档时间: |
|
| 查看次数: |
716 次 |
| 最近记录: |