ASP.NET MVC – 设置级联下拉菜单的选定值

发布于 2024-07-16 10:00:08 字数 3846 浏览 9 评论 0原文

我正在开发 ASP.NET MVC 应用程序并使用 NHibernate。

我正在研究级联下拉菜单,并在以下网站中使用了方法 1: 链接文本

一切正常,我只是想知道是否可以将级联下拉菜单设置为存储在数据库中的值? 例如,创建页面上的列表将为空,但在编辑页面上,所选值将设置为数据库中的值。

我更新了下面的代码:

CascadingDropDownList.js

 function bindDropDownList(e, targetDropDownList, selectedValue) {
var key = this.value;
var allOptions = targetDropDownList.allOptions;
var option;
var newOption;
targetDropDownList.options.length = 0;

for (var i = 0; i < allOptions.length; i++) {
    option = allOptions[i];
    if (option.key == key) {
        newOption = new Option(option.text, option.value,selectedValue);
        targetDropDownList.options.add(newOption);
    }
}

}

JavascriptExtenstions.cs

public static class JavaScriptExtensions
{
    public static string CascadingDropDownList(this HtmlHelper helper, string name, string associatedDropDownList)
    {
        var sb = new StringBuilder();

        // render select tag
        sb.AppendFormat("<select name='{0}' id='{0}'></select>", name.Replace("'",""));
        sb.AppendLine();

        // render data array
        sb.AppendLine("<script type='text/javascript'>");
        var data = (CascadingSelectList)helper.ViewDataContainer.ViewData[name.Replace("'","")];
        var listItems = data.GetListItems();
        var colArray = new List<string>();
        foreach (var item in listItems)
            colArray.Add(String.Format("{{key:'{0}',value:'{1}',text:'{2}'},selected:'{3}'}", item.Key, item.Value,item.Text.Replace("'",""),item.Selected));
        var jsArray = String.Join(",", colArray.ToArray());
        sb.AppendFormat("$get('{0}').allOptions=[{1}];", name.Replace("'",""), jsArray);
        sb.AppendLine();
        sb.AppendFormat("$addHandler($get('{0}'), 'change', Function.createCallback(bindDropDownList, $get('{1}')));", associatedDropDownList, name.Replace("'",""));
        sb.AppendLine();
        sb.AppendLine("</script>");
        return sb.ToString();

    }
}
public class CascadingSelectList
{
    private IEnumerable _items;
    private string _dataKeyField;
    private string _dataValueField;
    private string _dataTextField;
    private string _dataSelected;


    public CascadingSelectList(IEnumerable items, string dataKeyField, string dataValueField, string dataTextField, string dataSelected)
    {
        _items = items;
        _dataKeyField = dataKeyField;
        _dataValueField = dataValueField;
        _dataTextField = dataTextField.Replace("'","");
        _dataSelected = dataSelected;
    }

    public List<CascadingListItem> GetListItems()
    {
        var listItems = new List<CascadingListItem>();
        foreach (var item in _items)
        {
            var key = DataBinder.GetPropertyValue(item, _dataKeyField).ToString();
            var value = DataBinder.GetPropertyValue(item, _dataValueField).ToString();
            var text = DataBinder.GetPropertyValue(item, _dataTextField).ToString().Replace("'","");
            var selected = DataBinder.GetPropertyValue(item, _dataSelected).ToString();
            listItems.Add(new CascadingListItem(key, value, text.Replace("'",""),selected));
        }
        return listItems;
    }
}

public class CascadingListItem
{
    public CascadingListItem(string key, string value, string text, string selected)
    {
        this.Key = key;
        this.Value = value;
        this.Text = text.Replace("'","");
        this.Selected = selected;
    }

    public string Key { get; set; }
    public string Value { get; set; }
    public string Text { get; set; }
    public string Selected { get; set; }
}

I'm working on an ASP.NET MVC application and using NHibernate.

I’m working on a cascading drop-down and have used Method 1 in the following website: link text

Everything is working correctly, I just wondered if it was possible to set the cascading drop-down to a value stored in a database? For example the list would be blank on a create page, but on an edit page the selected value would be set to a value in a database.

I have updated the code below:

CascadingDropDownList.js

 function bindDropDownList(e, targetDropDownList, selectedValue) {
var key = this.value;
var allOptions = targetDropDownList.allOptions;
var option;
var newOption;
targetDropDownList.options.length = 0;

for (var i = 0; i < allOptions.length; i++) {
    option = allOptions[i];
    if (option.key == key) {
        newOption = new Option(option.text, option.value,selectedValue);
        targetDropDownList.options.add(newOption);
    }
}

}

JavascriptExtenstions.cs

public static class JavaScriptExtensions
{
    public static string CascadingDropDownList(this HtmlHelper helper, string name, string associatedDropDownList)
    {
        var sb = new StringBuilder();

        // render select tag
        sb.AppendFormat("<select name='{0}' id='{0}'></select>", name.Replace("'",""));
        sb.AppendLine();

        // render data array
        sb.AppendLine("<script type='text/javascript'>");
        var data = (CascadingSelectList)helper.ViewDataContainer.ViewData[name.Replace("'","")];
        var listItems = data.GetListItems();
        var colArray = new List<string>();
        foreach (var item in listItems)
            colArray.Add(String.Format("{{key:'{0}',value:'{1}',text:'{2}'},selected:'{3}'}", item.Key, item.Value,item.Text.Replace("'",""),item.Selected));
        var jsArray = String.Join(",", colArray.ToArray());
        sb.AppendFormat("$get('{0}').allOptions=[{1}];", name.Replace("'",""), jsArray);
        sb.AppendLine();
        sb.AppendFormat("$addHandler($get('{0}'), 'change', Function.createCallback(bindDropDownList, $get('{1}')));", associatedDropDownList, name.Replace("'",""));
        sb.AppendLine();
        sb.AppendLine("</script>");
        return sb.ToString();

    }
}
public class CascadingSelectList
{
    private IEnumerable _items;
    private string _dataKeyField;
    private string _dataValueField;
    private string _dataTextField;
    private string _dataSelected;


    public CascadingSelectList(IEnumerable items, string dataKeyField, string dataValueField, string dataTextField, string dataSelected)
    {
        _items = items;
        _dataKeyField = dataKeyField;
        _dataValueField = dataValueField;
        _dataTextField = dataTextField.Replace("'","");
        _dataSelected = dataSelected;
    }

    public List<CascadingListItem> GetListItems()
    {
        var listItems = new List<CascadingListItem>();
        foreach (var item in _items)
        {
            var key = DataBinder.GetPropertyValue(item, _dataKeyField).ToString();
            var value = DataBinder.GetPropertyValue(item, _dataValueField).ToString();
            var text = DataBinder.GetPropertyValue(item, _dataTextField).ToString().Replace("'","");
            var selected = DataBinder.GetPropertyValue(item, _dataSelected).ToString();
            listItems.Add(new CascadingListItem(key, value, text.Replace("'",""),selected));
        }
        return listItems;
    }
}

public class CascadingListItem
{
    public CascadingListItem(string key, string value, string text, string selected)
    {
        this.Key = key;
        this.Value = value;
        this.Text = text.Replace("'","");
        this.Selected = selected;
    }

    public string Key { get; set; }
    public string Value { get; set; }
    public string Text { get; set; }
    public string Selected { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作妖 2024-07-23 10:00:08

您需要修改扩展方法以包含选定的值,如普通下拉列表。

您还需要修改 javascript:

function bindDropDownList(e, targetDropDownList)  // Need to add extra parameter here.
{
    var key = this.value;
    var allOptions = targetDropDownList.allOptions;
    var option;
    var newOption;
    targetDropDownList.options.length = 0;

    for (var i=0; i < allOptions.length; i++) 
    {
        option = allOptions[i];
        if (option.key == key) 
        {
            newOption = new Option(option.text, option.value); // Need to set to selected here
        targetDropDownList.options.add(newOption);
    }
}

}

您需要将额外的参数传递给此处的 javascript 函数:

sb.AppendFormat("$addHandler($get('{0}'), 'change', Function.createCallback(bindDropDownList, $get('{1}')));", associatedDropDownList, name);

You'll need to modify the extension method to include a selected value like the normal dropdown.

You'll also need to modify the javascript:

function bindDropDownList(e, targetDropDownList)  // Need to add extra parameter here.
{
    var key = this.value;
    var allOptions = targetDropDownList.allOptions;
    var option;
    var newOption;
    targetDropDownList.options.length = 0;

    for (var i=0; i < allOptions.length; i++) 
    {
        option = allOptions[i];
        if (option.key == key) 
        {
            newOption = new Option(option.text, option.value); // Need to set to selected here
        targetDropDownList.options.add(newOption);
    }
}

}

You'll need to pass in the extra parameter to the javascript function here:

sb.AppendFormat("$addHandler($get('{0}'), 'change', Function.createCallback(bindDropDownList, $get('{1}')));", associatedDropDownList, name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文