开发者

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

开发者 https://www.devze.com 2023-02-18 10:24 出处:网络
i have two html.actionlinks: <%= Html.ActionLink(\"Activate\", \"ActivateJob\", \"Management\", new { selectedObject = Model.ID }, new { @class = \"actions\" })%>

i have two 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 开发者_如何学Gois 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? I'm getting just a last result in both html.actionlinks but i need value of selected checkbox. i have many of them.

it is a action witch will be called from 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. 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. 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.

Also I would strongly recommend you using the HTTP POST or PUT verb for something that is modifying state on the server.


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):

<%@ 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:

Index.cshtml:

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

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消