【问题标题】:How to get the selected list of items from checkboxlist and pass it to the Action in MVC4如何从 checkboxlist 中获取选定的项目列表并将其传递给 MVC4 中的 Action
【发布时间】:2012-11-01 13:48:51
【问题描述】:

我正在尝试在我的 MVC 应用程序中实现一个复选框列表。在这里,我需要将选定的复选框列表值传递给@html.ActionLink()。
我搜索了许多站点,包括 Stackoverflow,但没有数据库值的模型绑定,也没有获取选定的复选框值并传递给操作。
Eg :https://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0

我从这个链接尝试过。 CheckBoxList multiple selections: how to model bind back and get all selections?

如何从复选框列表中获取选定的 productid。 任何例子.. ??

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 checkbox


    【解决方案1】:

    查看:

    @foreach (var item in Model.Tags)
    { <label>
          <input type="checkbox" name="tag" value="@item.TagID" @if (item.Selected) { <text>checked="checked"</text> } />@item.Name
      </label>
    }
    

    控制器:

        [HttpPost]
        public RedirectToRouteResult Edit(IEnumerable<Guid> tag)
        {
            using (var dc = new MyDataContext())
            {
                var existing = dc.Tag
                    .Select(i => i.TagID)
                    .ToList();
    
                // remove old
                foreach (var id in existing.Except(tags.EmptyIfNull()))
                    dc.Tag.DeleteOnSubmit(dc.Tag.Single(k => k.TagID == id);
    
                // add new
                foreach (var id in tags.EmptyIfNull().Except(existing))
                    dc.Tag.InsertOnSubmit(new Tag() { TagID = id, });
    
                dc.SubmitChanges();
            }
    
            return RedirectToAction("List");
        }
    

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多