查看:
@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" />
}
型号:
public class SegmentRepository
{
public List<String> GetSegmentation()
{
//I have the values in this
}
}
控制器:
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);
}
}
我无法在roleValue1
中获取选择选项值。
我想从roleValue1
获取值并使用它在我的视图中触发另一个下拉列表。
答案 0 :(得分:3)
查看代码
@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>
控制器代码
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; }
}
答案 1 :(得分:2)
如果你看一下<select> Tag
的语法就像这样
<select>
<option value="volvo">Volvo</option>
</select>
您必须设置每个选项的值,但在您的情况下,您只是提供文本而不是值。所以它应该是这样的
@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" />
}
答案 2 :(得分:1)
目前,您有2个问题:
1)option
元素中的select
标记要求使用value
个实例对FormCollection
属性进行后期处理。
2)BeginForm
应使用适当的控制器名称,并在相应的控制器操作方法上标记HttpPost
属性(默认控制器操作方法使用HttpGet
)。
正确的用法应该是这样的:
查看强>
@using (Html.BeginForm("Index", "API", FormMethod.Post))
{
<select id="Segmentation" name="Segmentation">
@foreach (var item in Model.listofSegments)
{
<option value="@item">@item</option>
}
</select>
<input type="submit" value="Send" />
}
<强>控制器强>
public class APIController : Controller
{
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
dynamic mymodel = new ExpandoObject();
SegmentRepository segment = new SegmentRepository();
mymodel.listofSegments = segment.GetSegmentation();
String roleValue1 = formCollection["Segmentation"];
return View(mymodel);
}
}
如果您想将FormCollection
传递给动态ExpandoObject
,可以考虑使用以下帖子:FormCollection to expandoObject。
此外,如果要传递select
选项值以将选项值插入到同一视图页面的其他select
元素中,则需要在客户端使用jQuery AJAX回调($.ajax
)传递数据&amp;使用append
方法在option
响应后添加success
元素。
答案 3 :(得分:0)
你必须改变:
@using(Html.BeginForm(&#34; Index&#34;,&#34; APIController&#34;,FormMethod.Post))
为:
@using(Html.BeginForm(&#34; Index&#34;,&#34; API&#34;,FormMethod.Post))