简体   繁体   English

如何获取 HTML.ActionLink 中一个或多个复选框的值?

[英]how to get value of one or more checkboxes in HTML.ActionLink?

i have two html.actionlinks:我有两个 html.actionlinks:

<%= Html.ActionLink("Activate", "ActivateJob", "Management", new { selectedObject = Model.ID }, new { @class = "actions" })%>
            |
            <%= Html.ActionLink("Deactivate", "DeactivateJob", "Management", new { selectedObject = Model.ID }, new { @class = "actions" })%>

here is part of my table:这是我表的一部分:

 foreach (WPM.Logic.Job item in this.Model.Jobs)
                    {
                        Model.ID = item.ID;
                %>
                <tr style="background-color: #FEF0D7">
                    <td style="border-bottom: solid 1px #f3ad44; width: 80px;" align="center">
                        <%= i = i + 1 %>
                    </td>
                    <td style="border-bottom: solid 1px #f3ad44; width: 120px;">
                        <input type="checkbox"  name="selectedObject" value="<%= Model.ID %>" />
                    </td>

in page source i have follow results:在页面源中,我有以下结果:

 <td style="border-bottom: solid 1px #f3ad44; width: 120px;"> 
                        <input type="checkbox"  name="selectedObject" value="8cdc5c7a-72ba-4883-99b9-272c866c27a9" /> 
                    </td> 

<td style="border-bottom: solid 1px #f3ad44; width: 120px;"> 
                        <input type="checkbox"  name="selectedObject" value="fa6b304c-9eee-483f-8208-e2febd077e50" /> 
                    </td> 

question is: how to get these two checkbox values in HTML.ActionLink selectedObject?问题是:如何在 HTML.ActionLink selectedObject 中获取这两个复选框值? I'm getting just a last result in both html.actionlinks but i need value of selected checkbox.我在两个 html.actionlinks 中都只得到最后一个结果,但我需要选定复选框的值。 i have many of them.我有很多。

it is a action witch will be called from html.actionlink.这是一个从 html.actionlink 调用的动作女巫。

[HttpGet]
        public ActionResult ActivateJob(Guid[] selectedObject)
        {
            foreach (Guid guid in selectedObject)
            {

            }
            return View();
        }

        [HttpGet]
        public ActionResult DeactivateJobs(Guid[] selectedObject)
        {
            foreach (Guid guid in selectedObject)
            {

            }
            return View();
        }

Checkboxes usually go along with HTML forms, not action links.复选框通常与 HTML 表单一起使用,而不是操作链接。 So put them inside a form and use a submit button which will automatically send the checked values to the corresponding controller action.因此,将它们放在表单中并使用提交按钮,该按钮将自动将选中的值发送到相应的控制器操作。 If you want to use links you will need to write javascript code that will subscribe for the click event of the link, extract the values of checkboxes, modify the URL this link is pointing to in order to append those values to the query string which IMHO is too much of a work for something so simple.如果您想使用链接,您将需要编写 javascript 代码来订阅链接的点击事件,提取复选框的值,修改此链接指向的 URL,以便将这些值附加到恕我直言的查询字符串对于这么简单的事情来说,工作量太大了。 Of course you can have multiple submit buttons with different names inside a single HTML <form> and in the corresponding controller action you will be able to get the name of the button that was clicked so that you could perform different action.当然,您可以在单个 HTML <form>拥有多个具有不同名称的提交按钮,并且在相应的控制器操作中,您将能够获得被单击的按钮的名称,以便您可以执行不同的操作。

Also I would strongly recommend you using the HTTP POST or PUT verb for something that is modifying state on the server.此外,我强烈建议您使用 HTTP POST 或 PUT 动词来修改服务器上的状态。


UPDATE:更新:

As requested in the comments section I include an example.根据评论部分的要求,我提供了一个示例。

As always you start with a model:与往常一样,您从模型开始:

public class JobViewModel
{
    public string Guid { get; set; }
    public bool Selected { get; set; }
}

public class MyViewModel
{
    public IEnumerable<JobViewModel> Jobs { get; set; }
}

then you move on the controller:然后你移动控制器:

public class JobsController: Controller
{
    public ActionResult Edit()
    {
        var model = new MyViewModel
        {
            // Obviously those will be coming from some data store
            // and you could use AutoMapper to map your business entities
            // to the corresponding view model
            Jobs = new[]
            {
                new JobViewModel { ID = Guid.NewGuid() },
                new JobViewModel { ID = Guid.NewGuid() },
                new JobViewModel { ID = Guid.NewGuid() },
            }
        };
        return View(model);
    }

    [HttpPut]
    public ActionResult Update(MyViewModel model, string activate)
    {
        if (!string.IsNullOrEmpty(activate)) 
        {
            // the Activate button was clicked
        }
        else
        {
            // the Deactivate button was clicked
        }
        // TODO: model.Jobs will contain the checked values => 
        // do something with them like updating a data store or something

        // TODO: return some view or redirect to a success action
        return View("Edit", model);
    }
}

then you would have a strongly typed view in which you will use editor templates:那么您将拥有一个强类型视图,您将在其中使用编辑器模板:

<% using (Html.BeginForm("Update", "Jobs")) { %>
    <%= Html.HttpMethodOverride(HttpVerbs.Put) %>
    <table>
        <thead> 
            <tr>
                <th>Foo bar column ...</th>
            </tr>
        </thead>
        <tbody>
            <%= Html.EditorFor(x => x.Jobs) %>
        </tbody>
    </table>
    <input type="submit" value="Activate" name="activate" />
    <input type="submit" value="Dectivate" name="deactivate" />
<% } %>

and the last part would be the corresponding editor template which will be rendered for each item in the Jobs collection ( ~/Views/Jobs/EditorTemplates/JobViewModel.ascx ):最后一部分将是相应的编辑器模板,它将为 Jobs 集合中的每个项目呈现 ( ~/Views/Jobs/EditorTemplates/JobViewModel.ascx ):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.JobViewModel>" 
%>
<tr>
    <%= Html.HiddenFor(x => x.ID) %>
    <%= Html.CheckBoxFor(x => x.Selected) %>
</tr>

Maybe I am complete out of scope.也许我完全超出了范围。 But I have solved my problem of "Simulating a checkbox behavior with an ActionLink" in the following (dirty) way using two ASCII-Characters to visualize my two states:但是我已经使用两个 ASCII 字符以以下(脏)方式解决了“使用 ActionLink 模拟复选框行为”的问题,以可视化我的两个状态:

Index.cshtml:索引.cshtml:

@Html.ActionLink($"{(HomeController.IsExpertMode ? "☑️" : "⬜")}Expert-Mode", "ToggleExpertMode", "Home")

HomeController.cs: HomeController.cs:

public class HomeController : Controller
{
    ...

    public bool IsExpertMode { get; private set; }

    public ActionResult ToggleExpertMode()
    {
      IsExpertMode = !IsExpertMode;
      return RedirectToAction("Index");
    }

    ...
}

Hopefully this can help somebody searching for a simple solution for that problem - which brought me on this page, too...希望这可以帮助某人为该问题寻找简单的解决方案 - 这也将我带到了这个页面......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM