2017-08-28 54 views
2

查看:如何從一個視圖傳遞一個選擇選項的值到控制器中MVC5

@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

有你想使用級聯HTML'select'?如果我的猜測是正確的,請考慮使用客戶端JS觸發AJAX調用控制器動作並從那裏返回響應數據(可能使用部分視圖或AJAX「成功」,它可以爲第二個下拉菜單添加選項元素)。 –

回答

1

,如果你看看<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" /> 
} 
0

你必須改變:

@using(Html.BeginForm( 「指數」, 「APIController」,FormMethod.Post))

到:

@using(Html.BeginForm( 「指數」, 「API」,FormMethod.Post))

+0

那麼我的控制器的名稱是APIController – Sid

+0

在你的情況下,只有API是必要的。 –

0

目前你有2個問題在這裏:

1)select元素內的option標籤要求value屬性用FormCollection實例進行POST-ed。

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)與傳遞的數據&使用append方法添加option元素迴應後success

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; } 
} 
相關問題