如何在 mvc 2 的后期操作中获取选定的复选框值

发布于 2024-11-27 15:07:46 字数 1341 浏览 2 评论 0原文

我有一个从数据库填充的复选框列表,我想在后期操作期间获取每个复选框列表的 ID,以便我可以将其保存在数据库中,下面是代码: 控制器:

  public ActionResult Create()
    {
        ITrackdayRepository trackdayResp = new TrackdayRepository();
        IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
        var m = new SelectList(getAllEvents,"ID","Name");
        ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
        return View();
    } 

    //
    // POST: /Admin/Voucher/Create

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            // Get all the selected checkboxlist, do db insertion

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

视图

<label>Events</label>
        </td>
        <td>
         <% foreach (var item in (SelectList)ViewData["events"]) { %>
                 <input type="checkbox" name="Name" value="<%=item.Value %>" />
                  <label for="<%=item.Value%>"><%=item.Text%></label>
                  <br />

        <% } %>  
        </td>

我想传递选定的 <%=item.Value %>将复选框列表添加到 create 的 post aqction 中,以便我可以像 1,2,3,4 一样保存它。

I have got a checkbox list populated from database , I want to get the ID of each checkbox list during post action so that I can save that in the db , Below is the Code :
Controller:

  public ActionResult Create()
    {
        ITrackdayRepository trackdayResp = new TrackdayRepository();
        IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
        var m = new SelectList(getAllEvents,"ID","Name");
        ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
        return View();
    } 

    //
    // POST: /Admin/Voucher/Create

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            // Get all the selected checkboxlist, do db insertion

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

View

<label>Events</label>
        </td>
        <td>
         <% foreach (var item in (SelectList)ViewData["events"]) { %>
                 <input type="checkbox" name="Name" value="<%=item.Value %>" />
                  <label for="<%=item.Value%>"><%=item.Text%></label>
                  <br />

        <% } %>  
        </td>

I want to pass selected <%=item.Value %> of the checkbox list to the post aqction of create , so that i can save it like 1,2,3,4 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友谊不毕业 2024-12-04 15:07:46

如果您想在发布表单时仅传递选定的复选框,请执行以下操作[按照建议]:

var myAnswers = collection["name"];

然后迭代并保存它,或者您可以尝试这种方式

ASP.Net MVC 复选框列表

if you want to pass only the selected checkboxes when you post the form then do the following [as suggested]:

var myAnswers = collection["name"];

and then iterate through it and save it or you can try this way to

ASP.Net MVC List of Checkboxes

久而酒知 2024-12-04 15:07:46

非常简单。在控制器中 Action 方法的参数列表中使用 FormCollection,然后为模型中的 CheckBoxBox 值创建一个字符串数组。

现在分配 formvalue["Your_CheckBoxBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

到控制器中新创建的字符串数组........

     public ActionResult Create()
{
    ITrackdayRepository trackdayResp = new TrackdayRepository();
    IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
    var m = new SelectList(getAllEvents,"ID","Name");
    ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
    return View();
} 

//
// POST: /Admin/Voucher/Create

[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        // Get all the selected checkboxlist, do db insertion
        model.CheckBoxValues=collection["Your_CheckBox_valueOnView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);




        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Its very simple . Use FormCollection in your Parameter list of Action method in your controller and then create a String Array for your CheckBoxBox values in your model .

Now Assign formvalue["Your_CheckBoxBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

to your newly created String Array in your Controller ........

     public ActionResult Create()
{
    ITrackdayRepository trackdayResp = new TrackdayRepository();
    IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
    var m = new SelectList(getAllEvents,"ID","Name");
    ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
    return View();
} 

//
// POST: /Admin/Voucher/Create

[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        // Get all the selected checkboxlist, do db insertion
        model.CheckBoxValues=collection["Your_CheckBox_valueOnView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);




        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
初懵 2024-12-04 15:07:46

所有分组的复选框都作为数组返回,即您只是

var myAnswers = collection["name"];
// split myAnswers here if required

在代码中请求的 1,4,8 还是我在这里遗漏了更大的东西?

all grouped checkboxes are returned as an array i.e 1,4,8 you just request

var myAnswers = collection["name"];
// split myAnswers here if required

in your code or am i missing something bigger here?

假装爱人 2024-12-04 15:07:46

非常简单,在控制器的 Action 方法的参数列表中使用 FormCollection ,然后为模型中的 CheckBoxBox 值创建一个字符串数组。

现在分配

formvalue["Your_CheckBoxBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

给控制器中新创建的字符串数组

public ActionResult Create()
{
    ITrackdayRepository trackdayResp = new TrackdayRepository();
    IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
    var m = new SelectList(getAllEvents,"ID","Name");
    ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
    return View();
} 

// POST: /Admin/Voucher/Create

[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        // Get all the selected checkboxlist, do db insertion
        model.CheckBoxValues=collection["Your_CheckBox_valueOnView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

It's very simple, use FormCollection in your Parameter list of Action method in your controller and then create a String Array for your CheckBoxBox values in your model.

Now Assign

formvalue["Your_CheckBoxBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

to your newly created String Array in your Controller

public ActionResult Create()
{
    ITrackdayRepository trackdayResp = new TrackdayRepository();
    IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
    var m = new SelectList(getAllEvents,"ID","Name");
    ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
    return View();
} 

// POST: /Admin/Voucher/Create

[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        // Get all the selected checkboxlist, do db insertion
        model.CheckBoxValues=collection["Your_CheckBox_valueOnView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文