如何将选择选项值从视图传递到MVC5中的控制器

时间:2017-08-28 04:12:15

标签: c# html asp.net-mvc asp.net-mvc-5

查看:

@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获取值并使用它在我的视图中触发另一个下拉列表。

4 个答案:

答案 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))