在页面刷新的动态链接的下拉菜单菜单、页面、链接、动态

2023-09-10 14:45:06 作者:啈福の旋嵂

我有以下情形。我有2个下拉菜单。在第一个选项上的document.ready加载。当用户作出在第一个下拉选择为第二个选项被加载。我使用jQuery来填充两种,一切工作桃色。

I have the following scenario. I have 2 drop downs. The options in the first one are loaded on document.ready. The options for the second one are loaded when the user makes a selection in the first drop down. I'm using jQuery to populate both and everything works peachy.

这些下拉菜单住,这是通过GET提交,并在同一页面返回的ActionResult的表单中。因此,这里是我的问题,一旦提交表单时,我需要的下拉菜单,以保持它们的值,以及填充相应的选项列表。

These drop downs live inside a form that is submitted via a GET and the same page is returned as the ActionResult. So here's my problem, once the form is submitted, I need the drop downs to maintain their values as well as populate the appropriate option lists.

我的想法迄今已解析出查询参数和设定值的方式。我不知道如何重新填充第二个下拉列表中,但。

My idea so far has been to parse out the query parameters and set the values that way. I'm not sure how to repopulate the second drop down list though.

注意这是一个简单的例子,其实我有一大堆的下拉菜单中所链接的几个层次深,所以我需要一个稍微通用的解决方案。

NOTE this is a simplified example and I actually have a whole bunch of drop downs that are chained several levels deep so I need a somewhat generic solution.

推荐答案

当我这样做,每次调用一个动作的时候,你需要重建模型值的列表。然后,模型还存储所选择的值,这是自动保留

When I have done this, each time you call an action, you need to rebuild the list of values in the model. Then, your model also stores the selected value, which is automatically retained.

下面是一个地址控制,我有一个状态下拉的例子。

Here's an example of an address control where I have a state drop down.

首先,我的模型

public class AddressModel
{
    [Required(ErrorMessage = "Address is required")]
    [DisplayName("Address Line 1")]
    public string Address1 { get; set; }

    [DisplayName("Address Line 2")]
    public string Address2 { get; set; }

    [Required(ErrorMessage = "City is required")]
    [DisplayName("City")]
    public string City { get; set; }

    [Required(ErrorMessage = "State is required")]
    [DisplayName("State")]
    public string State { get; set; }

    [Required(ErrorMessage = "Zip code is required")]
    [DisplayName("Zip Code")]
    [StringLength(5, ErrorMessage = "{0} must be five digits.")]
    [RegularExpression(@"\d{5}", ErrorMessage = "{0} must be five digits.")]
    public string ZipCode { get; set; }

    public Dictionary<string, string> States { get; set; }

    public SelectList StatesSelectList
    {
        get
        {
            return new SelectList(States, "Value", "Key");
        }
    }
}

这是我的行动,所有,但重要的部分省略

And here are my actions, all but the important parts omitted

public ActionResult DoSomething()
{
    var model = new AddressModel()
    {
        States = GetStateList();
    };

    return View(model);
}

[HttpPost]
public ActionResult DoSomething(AddressModel model)
{

    if (ModelState.IsValid)
    {
        //do stuff here
    }

    model.States = GetStateList();

    return View(model);
}

这是它的外观在我看来

<%: Html.LabelFor(x => x.State)%>
<%: Html.DropDownListFor(x => x.State, Model.StatesSelectList)%>
<%: Html.ValidationMessageFor(x => x.State)%>

由于属性 AddressModel.State 绑定到一个控制,保持其价值的每一个动作后调用时。

Because the property AddressModel.State is bound to a control, its value is retained each time the post action is called.