使用复选框将项添加到列表中

时间:2014-05-01 17:01:28

标签: asp.net asp.net-mvc vb.net asp.net-mvc-3 checkbox

我正在尝试使用预先填充列表的复选框,以便在选中项目复选框时执行操作。

在我的模型中,我有一个列表,使用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

在我看来,我使用@Html.CheckBoxFor(Function(m) i.AddThis, New With {.class = "ttip", .title = "Add This to the Sprint."})

为列表中的每个项目添加AddThis布尔值的复选框

然后在我的帖子控制器中我有这个:

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

我也试过For Each i In model.TaskList.Where(Function(m) m.AddThis = True)

但是当我选中复选框并点击提交时,它并不承认任何AddThis布尔都是真的。谁能让我知道我怎么能正确地做到这一点?

1 个答案:

答案 0 :(得分:4)

首先,您尚未发布在此方案中使用的整个视图和控制器操作方法。所以我在这里做了一些假设。

正如@Alan所说,这似乎是如何生成控件名称的问题。

这只是我快速创建的一个示例。

以下是一个类似于您创建的简单模型。

public class ModelToSubmit
{
    public List<TaskItem> TaskList { get; set; }
}
public class TaskItem
{
    public bool Addthis { get; set; }
    public string SomeProperty { get; set; }
}

以下是控制器操作方法的外观。

[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);
}

以下是观点。

@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" />
}

使用此模型并查看表格中的文本框和复选框列表,如下图所示。

Sample output

当我点击帖子按钮时,我得到了填充了taskItems的整个TaskList模型

这是我选择前两个项目并发布时快速监视窗口的快照。 代码在c#中,但您可以轻松地将其转换为vb.net。 Quick watch

希望这适合你。