0

Controller:

public ActionResult searchGroup(int groep)
        {
            var returnValue = convertWerkvorm(db.Werkvormens.Where(f => f.GroepenWerkvormID == groep).ToList());
            return View(returnValue);
        }

Partialview:

@model alina1617.Models.DropDownModel

<h2>Groepen</h2>
<div>
        <div><select id="@Html.IdFor(m => m.selectedItem)" name="@Html.NameFor(m => m.selectedItem)">
            @foreach (var groepModel in ViewBag.groepen)
            {
                <option value="@groepModel.id" title="@groepModel.Beschrijving">@groepModel.Naam</option>
            }
        </select>
        <input type="button" id="zoekgroep" value="Zoeken" onclick="location.href='@Url.Action("Create", "WerkvormController")'" />
        </div>
    </div>

I want to pass the id of the selected group to the controller when my button is clicked. Should I use Jquery or can I do this using something MVC related?

user3117628
  • 776
  • 1
  • 15
  • 35
  • If you're operating on the client side, then you have no access to anything MVC-related. Use JavaScript, possibly jQuery. – krillgar Dec 22 '16 at 15:07
  • How would I do that using Jquery? – user3117628 Dec 22 '16 at 15:08
  • There are a nearly infinite number of tutorials out there for how to send information to the server with jQuery. – krillgar Dec 22 '16 at 15:11
  • Possible duplicate of [Get the selected value of a DropDownList. Asp.NET MVC](http://stackoverflow.com/questions/15881575/get-the-selected-value-of-a-dropdownlist-asp-net-mvc) – geekzster Dec 22 '16 at 15:41

2 Answers2

0
@Html.DropDownListFor(x => x.Selectedvalue, new SelectList(ViewBag.groepen, "id", "Naam"), "Select", new { @class = "form-control" })

use dropdownfor and add Selectedvalue property in your Model, And put all this in form and make button input type submit

Ahmed
  • 1,542
  • 2
  • 13
  • 21
0

Whether you should do it with jQuery or MVC is subjective so I leave that to you. You can do it either way. Google search it, there are numerous examples.

As for "MVC related", have a look at something like this (just an example):

@Html.DropDownList("groep", (SelectList)ViewBag.Groepen)

You will want to wrap the list and button in a form tag and change your button type to submit as well. Your method in the controller may need the [HttpPost] atttribute if it isn't there already.

You could also look at the DropDownListFor object, which is similar.

I believe you might be asking something that has been answered a lot. Have a look at this post, and this one

Community
  • 1
  • 1
geekzster
  • 559
  • 8
  • 21