English 中文(简体)
发布后, 缓列下下下调会丢失选中的项目
原标题:cascading dropdown loses select items after post

我有一个MVC3剃刀项目,我正试图用多个可重复使用元素来建立这个项目。

public class ProductCreateModel
    {
        [Required]
        [Display(Name = "Org")]
        [UIHint("MyOrgsDropDown")]
        public string orgName { get; set; }

        [Required(ErrorMessage = "Select an account")]
        [Display(Name = "Account")]
        [UIHint("MyAccountsDropDown")]
        [AdditionalMetadata("orgFieldId", "orgName")]
        [AdditionalMetadata("fieldId", "accountName")]
        public string accountName { get; set; }
    }

我为这个例子略略略了其他一些字段

MyAccountsDropDown.cshtml看起来像这样:

@{  
var values = ViewData.ModelMetadata.AdditionalValues;
string orgFieldId = (string)values["orgFieldId"];
string fieldId = (string)values["fieldId"];
}
<script type="text/javascript">
$(document).ready(function () {
    $("#@orgFieldId").change(function () {
        var idProd = $(this).val();
        $.getJSON("/JsonService/GetAccounts", { orgId: idProd },
            function (myData) {
                var select = $("#@fieldId");
                select.empty(); 
                $.each(myData, function (index, itemData) {
                    select.append($( <option/> , {
                        value: itemData.Value,
                        text: itemData.Text
                    }));
                });
            });
    });
});
</script>


@Html.DropDownList("", new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "---Select---", new { id = fieldId })

我的主计长的方法看起来是这样的:

    [HttpGet]
    public ActionResult AddProduct()
    {
        return PartialView("_AddProduct");
    }

    [HttpPost]
    public ActionResult AddProduct(ProductCreateModel model)
    {
        if (!ModelState.IsValid)
        {
            return PartialView("_AddProduct", model);
        }

        //do some stuff

        return PartialView("_AddProduct");
    }

因此,当视图打开(作为 jQuery 对话框) 时,我已设置了全部的窗体。 如果我选择了一个正方形Name, 它会从数据库( 条形标记) 中提取账户名称。 既然我正在使用模式验证, 当我不填写所有要求的字段时, 并按下提交, 它会向我显示验证信件。 它也显示我提交之前已经输入的字段的预填值。 正在删除的是串装的向下下降值。 因此它会继续显示选中的正方形名称, 但账户向下下降会丢失数据库中之前填好的值 。

在点击提交之前, 我如何维持已经提取的选中项目?

最佳回答

将您的 Javascript 部分的代码修改为 :

  $("#@orgFieldId").change(function () {
      var idProd = $(this).val();
      $.getJSON("/JsonService/GetAccounts", { orgId: idProd },
          function (myData) {
              var select = $("#@fieldId");
              select.empty(); 
              $.each(myData, function (index, itemData) {
                  select.append($( <option/> , {
                      value: itemData.Value,
                      text: itemData.Text
                  }));
              });
          });
  });

  $(document).ready(function () {
    $("#@orgFieldId").change();
  });
问题回答

暂无回答




相关问题
AJAX, PHP, XML, and cascading drop-down lists

What PHP libraries would you recommend to implement the following: Three dependent drop-down lists Three XML data sources AJAX-based Essentially, I d like to create an XML database and wire up a ...

CascadingDropDownList not working with anonymous access

I use a CascadingDropDownList of the AJAXControlToolkit in a ASP.NET MCMS 2002 web application. The CascadingDropDownList works as expected until "Anonymous access" and "Integrated Windows ...

Reading data from a table (newbie MS Access)

I realize the newbieness of this question, but google searches are not helping me. I ve created an MS Access database and am trying to automatically update a cell in a row when another cell is ...

trigger change event in cascading dropdown

I am using cascading dropdown from http://stephenwalther.com/blog/archive/2008/09/07/asp-net-mvc-tip-41-creating-cascading-dropdown-lists-with-ajax.aspx. I need to set a value to the dropdown, and ...

CascadingDropDown taking two parameters

I have a page with 3 dropdownlist, 2nd and 3rd dropdownlist are added with CascadingDropDown. 3rd dropdownlist will take parameters from 1st and 2nd dropdownlist. So, in current example for ...

WPF ObjectDataProvider that supports linq filtering?

Is there an WPF ObjectDataProvider that supports "Where" I mean like in asp.net there is an option to have a dataprovider that supports defining the results? What I actually need is two ComboBoxes ...

热门标签