简体   繁体   English

第二个下拉列表选定项目在ASP.NET MVC中不会更改

[英]Second dropdown list selected item does not change in ASP.NET MVC

I'm a bit confused by the behavior of ASP.NET MVC with it not changing the value of a dropdown list after a POST. 我对ASP.NET MVC的行为感到有点困惑,因为它没有在POST后改变下拉列表的值。 Can someone explain how to do this. 有人可以解释如何做到这一点。

First of all I have a model that looks like this: 首先,我有一个看起来像这样的模型:

public class Test
{
    public int OneID { get; set; }
    public IEnumerable<SelectListItem> OneList
    {
        get
        {
            yield return new SelectListItem
            {
                Text = "Red",
                Value = "0"
            };
            yield return new SelectListItem
            {
                Text = "Green",
                Value = "1"
            };
            yield return new SelectListItem
            {
                Text = "Blue",
                Value = "2"
            };
        }
    }

    public int TwoID { get; set; }
    public IEnumerable<SelectListItem> TwoList
    {
        get
        {
            yield return new SelectListItem
            {
                Text = "Ruby",
                Value = "0"
            };
            yield return new SelectListItem
            {
                Text = "Gem",
                Value = "1"
            };
            yield return new SelectListItem
            {
                Text = "Bronze",
                Value = "2"
            };
        }
    }
}

The view has this snippet of code in the body content: 该视图在正文内容中包含以下代码段:

<% using (Html.BeginForm("Index", "Home", FormMethod.Post))
   { %>
<table>
    <tr>
        <td>
            <% =Html.DropDownList("OneID", Model.OneList, new 
                  { @onchange = "this.form.submit();" })%>
        </td>
        <td>
            <% =Html.DropDownList("TwoID", Model.TwoList)%>
        </td>
    </tr>
</table>
<% } %>

And the GET and POST actions look like this: GET和POST操作如下所示:

public ActionResult Index()
{
    Test test = new Test();
    test.OneID = 0;
    test.TwoID = 0;
    return View(test);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(Test test)
{
    test.TwoID = test.OneID;
    return View(test);
}

What I'm trying to achieve is to change the selected item in the second drop down to match the first drop down each time the first drop down is changed. 我想要实现的是在每次更改第一个下拉列表时,在第二个下拉列表中更改所选项目以匹配第一个下拉列表。 So I'm getting the selected value from the first drop down and assigning it to the ID used for the second drop down and then returning the same model. 因此,我从第一个下拉列表中获取所选值,并将其分配给用于第二个下拉列表的ID,然后返回相同的模型。 The first drop down is correctly set in the view but not the second one. 第一个下拉列表在视图中正确设置,但不在第二个下拉列表中正确设置。

Ideas? 想法?

The reason this doesn't work the way you expect it is because when the DropDownList helper tries to render the select element it first looks for posted values that match the name which in this case is TwoID and use this value instead of the value in the model. 这不能按预期方式工作的原因是因为当DropDownList帮助程序尝试呈现select元素时,它首先查找与name匹配的已发布值,在本例中为TwoID并使用此值而不是模型。 This is the same with a TextBox for example. 例如,这与TextBox相同。 If you use this helper and in the POST controller action you modify the value of the model it will still show the old value. 如果您使用此帮助程序并在POST控制器操作中修改模型的值,它仍将显示旧值。 That's just how helpers work. 这就是帮助者的工作方式。 They bind from POSTed values. 它们从POSTed值绑定。

There are two possible workarounds: 有两种可能的解决方法:

  1. Use javascript to synchronize both dropdowns: 使用javascript同步两个下拉菜单:

     @onchange = "document.getElementById('TwoID').value = this.value; this.form.submit();" 

    BTW this should be done in an unobtrusive way in a separate javascript file: 顺便说一句,这应该在一个单独的javascript文件中以不引人注目的方式完成:

     $(function() { $('#OneID').change(function() { $('#TwoID').val($(this).val()); this.form.submit(); }); }); 
  2. Write your own helper or generate the select manually (less recommended) 编写自己的助手或手动生成选择(少推荐)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM