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.
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; }
}
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" />
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With