Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a select option value from a view to a controller in MVC5

View:

@using (Html.BeginForm("Index", "APIController",FormMethod.Post))
{
    <select id="Segmentation" name="Segmentation">
    @foreach (var item in Model.listofSegments)
    {
        <option>@item</option>
    }
    </select>
    <input type="submit" value="Send" />
}

Model:

public class SegmentRepository
{
    public List<String> GetSegmentation()
    {
        //I have the values in this
    }
}

Controller:

public class APIController : Controller
{
    public ActionResult Index(FormCollection formCollection)
    {
        dynamic mymodel = new ExpandoObject();
        SegmentRepository segment = new SegmentRepository();
        mymodel.listofSegments = segment.GetSegmentation();
        String roleValue1 = formCollection["Segmentation"];
        return View(mymodel);
    }
}

I am not able to get the select option value in roleValue1.

I want to take the value from roleValue1 and use it to trigger another dropdown in my view.

like image 249
Sid Avatar asked Jan 29 '23 17:01

Sid


2 Answers

View Code

@model dynamic
@{
    ViewBag.Title = "ddl";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>ddl</h2>

<div>
    @using (Html.BeginForm("ActionPostData", "Demo", FormMethod.Post))
{
    <select name="Segmentation">
        <option selected value="0">---Select---</option>
        @foreach (var item in Model)
        {
            <option value="@item.Value">@item.Text</option>
        }
    </select>
            <input type="submit" value="Send" />
}
</div>

Controller Code

public ActionResult Ddl()
{
    var segmentList = new List<listofSegments>();
    listofSegments segmentItem;
    var strArr = new string[] { "Jaipur", "Kota", "Bhilwara", "Udaipur", "Chitorgar", "Ajmer", "Jodhpur" };
    for (int index = 0; index < strArr.Length; index++)
    {
        segmentItem = new listofSegments();
        segmentItem.Text = strArr[index];
        segmentItem.Value = (index + 1).ToString();
        segmentList.Add(segmentItem);
    }
    return View(segmentList);
}

[HttpPost]
public ActionResult ActionPostData(string Segmentation)
{
    return RedirectToAction("Ddl");
}

public class listofSegments
{
    public string Text { get; set; }
    public string Value { get; set; }
}
like image 54
Manish Kumar Gurjar Avatar answered Feb 02 '23 11:02

Manish Kumar Gurjar


if you look at the syntax of <select> Tag its like this

<select>
  <option value="volvo">Volvo</option>
</select>

you have to set the value of each option but in your case you are just providing the text not the value. So it should be like this

@using (Html.BeginForm("Index", "APIController",FormMethod.Post))
{
    <select id="Segmentation" name="Segmentation">
    @foreach (var item in Model.listofSegments)
    {
        <option value="@item">@item</option> //if the value is same as text
    }
    </select>
    <input type="submit" value="Send" />
}
like image 28
Usman Avatar answered Feb 02 '23 09:02

Usman