1

I want to pass data from the view to controller with Html.beginform. The caregiverid is null. This should work but I don't know what I do wrong. I also checked the variable names, but it is the same.

I also tested, if the viewbag is filled with data. The viewbag is filled with the correct data.

This is the view

@using (Html.BeginForm("PairClient", "Admin", FormMethod.Post, new { @class = "col-md-12", caregiverid = ViewBag.caregiverid }))
{
    <select name="clientselect" class="selectpicker" data-live-search="true" data-actions-box="true" data-selected-text-format="count > 4" data-multiple-separator=" | " data-none-selected-text="Niks geselecteerd" data-deselect-all-text="Niks" data-select-all-text="Alles" data-count-selected-text="{0} clienten geselecteerd" data-width="100%" data-size="10" multiple>
        @foreach (var client in Model)
        {
            <option style="font-size:15px;" value="@client.ClientId" selected="@(client.IsSelected ? "selected" : null)" data-tokens="@client.Email">@client.SocialNumber - @client.Firstname @client.Lastname</option>
        }
    </select>
    <input class="btn btn-success " type="submit" value="Koppel cliënt" />
}

This is the controller


[HttpPost]
public IActionResult PairClient(string caregiverid, string[] clientselect)
{
    string token = Request.Cookies["userToken"];
    admin.PairClient(token, caregiverid, clientselect);
    return RedirectToAction("Singlecaregiver");
}

It worked! I do not know what I have changed... but somehow it worked

  • Your controller needs to accept a view model, not 2 seperate variables – Jake Steffen Jun 04 '19 at 13:07
  • This might be a duplicate of this: https://stackoverflow.com/questions/20333021/asp-net-mvc-how-to-pass-data-from-view-to-controller – Jake Steffen Jun 04 '19 at 13:16
  • Possible duplicate of [How does a multiple select list work with model binding in ASP.NET MVC?](https://stackoverflow.com/questions/1255472/how-does-a-multiple-select-list-work-with-model-binding-in-asp-net-mvc) – Ryan Wilson Jun 04 '19 at 13:16
  • I just want to have the caregiverId, This has nothing to do with the option values – TheProgrammer Jun 04 '19 at 13:16
  • 2
    @TheProgrammer You should add that to the question. It wasn't immediately clear. –  Jun 04 '19 at 13:23
  • You could use an ajax call. Do an onSubmit on the Html form and if you just want to send the caregiverId, use a json and send it to a controller method using ajax – JamesS Jun 04 '19 at 15:13
  • Within the `using` block, add `@Html.Hidden("caregiverid", ViewBag.caregiverid)` – akerra Jun 04 '19 at 15:37

3 Answers3

0

Try adding a hidden HTML tag eg.

<input type="hidden" name="caregiverid" id="caregiverid" value="@ViewBag.caregiverid"/>

In your controller, you are expecting a string array of client selected, but is the data in the select field an array?

Try and set the "multiple" attribute on your select element, or write each selection to a hidden field with and post it make sure each element has the same name as the method signature. eg.

<input type="hidden" name="clientselect"/>

Also on your controller update it to this and try

public IActionResult PairClient([FromForm] string caregiverid, [FromForm] string[] clientselect)
Bradley Petersen
  • 155
  • 1
  • 10
0

Hi I tried your Code and just modified it a bit and managed to get the data moved to the controller so please just copy paste the following code and all will be well

HTML

@using WebApplication1.Models;
@model IReadOnlyCollection<Client>`@{
ViewBag.Title = "Index";
}
<h2>Index</h2>`
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "col-md-12" }))
{ 
<select name="clientselect" class="selectpicker" data-live-search="true" data-actions-box="true" data-selected-text-format="count > 4" data-multiple-separator=" | " data-none-selected-text="Niks geselecteerd" data-deselect-all-text="Niks" data-select-all-text="Alles" data-count-selected-text="{0} clienten geselecteerd" data-width="100%" data-size="10" multiple>
       @foreach (var client in Model)
    {
        <option style="font-size:15px;" value="@client.ClientId" selected="@(client.IsSelected ? "selected" : null)" 
                data-tokens="@client.Email">@client.SocialNumber - @client.Firstname @client.Lastname</option>
    }
    </select>
<input type="hidden" name="caregiverid" id="caregiverid" value="@ViewBag.caregiverid" />
<input class="btn btn-success " type="submit" value="Koppel cliënt" />
}

enter image description here This is an image of the controller that i added to the project and as you can see caregiverid is set to 1 and two clients have been selected. hope this solved your problem.

But i do recommend that you change from that code in the controller to something similar to this enter image description here

0

Try adding <select name="clientselect[]" ... (notice []) to your select tag.

Like here: https://support.aspnetzero.com/QA/Questions/8725/How-to-add-a-multi-select-drop-down-in-Modal

Josip P
  • 1
  • 2