【问题标题】:Understanding how DropDownListFor is working in MVC3了解 DropDownListFor 如何在 MVC3 中工作
【发布时间】:2011-02-23 23:56:05
【问题描述】:

我是 MVC3 的新手,一直在使用 EF 和“代码优先”在一个小型站点上工作。我正在尝试在处理下拉列表的视图中做一些事情,并且想知道处理它们的最佳方法是什么。我希望用户能够从下拉列表中选择规则,并且根据选择的规则,我希望页面上的标签显示规则名称(不发布)。我还需要能够将所选规则发送到下一页。我还没有将所有必要的字段添加到视图中,因为我真的不知道它应该如何工作。我应该如何尝试这样做?

我有我的模型:

public class D1K2N3CARule
{
    public int ID { get; set; }
    [Required]
    public int Name { get; set; }
    [Required]
    public string Rule { get; set; }

    public D1K2N3CARule(int name, string rule)
    {
        Name = name;
        Rule = rule;
    }
    public D1K2N3CARule()
    {
        Name = 0;
        Rule = "";
    }
}

我的视图模型:

public class D1K2N3CARuleViewModel
{
    public string SelectedD1K2N3CARuleId { get; set; }
    public IEnumerable<D1K2N3CARule> D1K2N3CARules { get; set; }
}

我的控制器:

    public ActionResult Index()
    {
        var model = new D1K2N3CARuleViewModel
        {
            D1K2N3CARules = db.D1K2N3DARules
        };

        return View(model);
    }

和我的观点:

'@model CellularAutomata.Models.D1K2N3CARuleViewModel

@{
    ViewBag.Title = "Index";
}
<asp:Content id="head" contentplaceholderid="head" runat="server">
<script type="text/javascript">
</script>
</asp:Content>
<h2>Index</h2>

<table>
    <tr>
        <td>
            @Html.DropDownListFor(
                x => x.D1K2N3CARules,
                new SelectList(Model.D1K2N3CARules, "ID","Rule")
            )
        </td>
    </tr>
</table>'

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 drop-down-menu html.dropdownlistfor


    【解决方案1】:

    我希望用户能够从下拉列表中选择规则,并且根据选择的规则,我希望页面上的标签显示规则名称(不发布)

    您需要在这里使用 javascript。 jQuery 非常适合这项工作。我会首先为下拉菜单提供一个确定的 id,因为如果你在模板中运行这个视图,可能会在 id 中添加前缀,这会破坏我们的 javascript id 选择器(见下文):

    @Html.DropDownListFor(
        x => x.D1K2N3CARules,
        new SelectList(Model.D1K2N3CARules, "ID", "Rule"),
        new { id = "ruleDdl" }
    )
    

    然后提供一些容器来接收选定的值:

    <div id="ruleValue" />
    

    最后在一个单独的 javascript 文件中订阅下拉列表的更改事件并使用所选值/文本更新容器:

    $(function() {
        // subscribe for the change event of the dropdown
        $('#ruleDdl').change(function() {
            // get the selected text from the dropdown
            var selectedText = $(this).find('option:selected').text();
    
            // if you wanted the selected value you could:
            // var selectedValue = $(this).val();
    
            // show the value inside the container
            $('#ruleValue').html(selectedText);
        });
    });
    

    我还需要能够将所选规则发送到下一页。

    您可以将下拉列表放入表单中

    @using (Html.BeginForm("NextPage", "Foo"))
    {
        @Html.DropDownListFor(
            x => x.D1K2N3CARules,
            new SelectList(Model.D1K2N3CARules, "ID","Rule")
        )    
        <input type="submit" value="Go to the next page" />
    }
    

    NextPage 控制器动作将接收选定的值。

    【讨论】:

    • 什么参数被传回下一页控制器?我尝试的一切最终都通过null。在过去的几个小时里,我一直在四处寻找答案,但一直都是空的。您对 MVC3 上的好资源有什么建议吗?显然,我一直在转动我的轮子,我想我需要做一些阅读。
    • @Doug S.,只有您在表单中包含的内容才会传递给下一个控制器。在此示例中,我们只有一个下拉列表,因此只会发送选定的值。
    • 我明白,但我在我的下一页控制器中指定什么参数? public ActionResult RunCA(string id) or public ActionResult RunCA(selectlistitem item) or public ActionResult RunCA(int id) 我试过的一切都返回null。
    • @Doug S.:public ActionResult RunCA(string selectedD1K2N3CARuleId)。顺便说一句,您的 DropDownListFor 助手用法是错误的,因为您对所选值和项目使用相同的属性 (D1K2N3CARules)。应该是:@Html.DropDownListFor(x =&gt; x.SelectedD1K2N3CARuleId, new SelectList(Model.D1K2N3CARules, "ID","Rule"))。这将允许您在下一页上获取选定的值 (SelectedD1K2N3CARuleId)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多