1

我为传入视图的字符串列表中的每个项目动态生成 DropDownList,然后为一些标准选项生成下拉列表,下拉列表名称为字母“drp”+通过视图数据传入视图的字符串项目。我遇到的问题是我无法弄清楚如何访问视图中 HttpPost 中的单个下拉列表,因为项目的名称和数量各不相同。

这是我的视图的获取代码:

public ActionResult ModMapping()
        {

            ViewData["mods"] = TempData["mods"];
            return View();
        }

这是我的视图生成:

<% using (Html.BeginForm()) { %>

<h2>Modifier Needing Mapping</h2>
<p>Please Choose for each modifier listed below what type of fee it is.  There is an ingore option if it is not a gloabl fee modifier, professional fee modifier, or technical fee modifier.</p>
<table>
    <tr>
        <th>Modifier</th>
        <th>Mapping Options</th>
    </tr>
        <% int i; 
           i=0;
           var modsList = ViewData["mods"] as List<String>;%>

        <% foreach (String item in modsList) { %>
            <% i++; %>
            <tr>
                <td>
                    <%: Html.Label("lbl" + item, item) %>
                </td>
                <td>
                    <%: Html.DropDownList("drp" + item, new SelectList(
                  new List<Object>{ 
                       new { value = "NotSelected" , text = "<-- Select Modifier Type -->"},
                       new { value = "Global" , text = "Global Fee" },
                       new { value = "Professional" , text = "Professional Fee"},
                       new { value = "Technical", text = "Technical Fee"},
                       new { value = "Ingore", text="Ingore This Modifier"}
                    },
                  "value",
                  "text",
                  "NotSelected")) %>
                </td>
            </tr>
        <% } %>
</table>
 <input type="submit" value="Done" />
<% } %></code>
4

1 回答 1

0

如果您为视图使用模型,这非常简单,因为 MVC 会自动将所有下拉列表映射回您的模型。

如果你真的不想使用模型,你可以像这样访问表单中的值

// Load your modsList here.
foreach (String item in modsList)
{
   var dropDownValue = Request["drp" + item];
}

另一种选择是编写一个接受 FormCollection 的控制器函数,它只是 POST 中所有值的简单字典:

[HttpPost]
public ActionResult ModMapping(FormCollection formCollection)
{
    // Load your modsList here.
    foreach (String item in modsList)
    {
       var dropDownValue = formCollection["drp" + item];
    }
}

您可能可以简化一些事情,并通过 FormCollection 循环查找以“drp”开头的项目,具体取决于您的页面和要求的外观。

于 2013-04-09T18:04:19.383 回答