0

我需要一些关于从肥皂网络服务获取值以使用视图模型显示在下拉列表中的建议,我目前从服务类库项目(n 层应用程序)中找到的服务类接收各种下拉列表的数据。

下拉列表服务的代码遵循与以下代码类似的格式:

public IEnumerable<SelectListItem> getValuesAsSelectItems(string selected)
    {
        var items = new List<SelectListItem>();

        items.Add(new SelectListItem { Text = "Please Select", Value = string.Empty, Selected = (selected == string.Empty) });

        foreach (var value in this.getValues())
        {
            items.Add(new SelectListItem { Text = value.Value, Value = value.Value, Selected = (selected == value.Value) });
        }
        return new SelectList(items, "Value", "Text");
    }

我需要一种将值从该服务传递到视图模型的方法,然后我为每个下拉列表创建了 7 个控制器,它们都将链接到我可以在整个应用程序中重用的部分视图,下拉列表包括标题、国家、州和其他。

4

1 回答 1

1

您可以采取的一种方法是将下拉列表值提取到它们自己的视图模型中。所以:

第 1 步:创建一个ItemsViewModel封装下拉列表项的视图模型 ( ):

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Models
{
    public class DropDownListItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

    public class ItemsViewModel
    {
        private readonly List<DropDownListItem> _items;

        // The selected item:
        public string SelectedItem { get; set; }

        // The items:
        public IEnumerable<SelectListItem> Items
        {
            get
            {
                var allItems = _items.Select(i => new SelectListItem 
                {
                    Value = i.Value,
                    Text = i.Text
                });
                return DefaultItem.Concat(allItems);
            }
        }

        // Default item (i.e. the "select" text) if none selected
        public IEnumerable<SelectListItem> DefaultItem
        {
            get
            {
                return Enumerable.Repeat(new SelectListItem
                {
                    Value = "-1",
                    Text = "Select an item"
                }, count: 1);
            }
        }

        public ItemsViewModel()
        {

        }

        // Constructor taking in items from service and selected item string:
        public ItemsViewModel(List<DropDownListItem> items, string selected)
        {
            _items = items;
            SelectedItem = selected;
        }
    }
}

第 2 步:在 Views 文件夹中创建一个局部视图,该视图绑定到ItemsViewModel

@model Models.ItemsViewModel
@Html.DropDownListFor(m => m.SelectedItem, Model.Items)

第 3 步:在适当的控制器(例如 HomeController)中,将从服务、视图模型和局部视图中提取数据的子操作放在一起:

[ChildActionOnly]
public ActionResult DropDownList(string type, string selected)
{
    // If you need to call different services based on the type (e.g. Country), then pass through "type" and base the call on that

    var items = new ItemsViewModel(
        (from g in _service.getTitles() select new DropDownListItem { Text = g.Text, Value = g.Value }).ToList(), 
        selected);

    return PartialView("DropDownPartial", items);
}  

第 4 步:将这行代码拖放到需要下拉列表的视图中:

@Html.Action("DropDownList", "Home", new { selected = "2", type = "country" })

请注意,selectedandtype将根据您认为合适的方式来确定,并且是可选的。

希望这能给你一些启发。

于 2013-03-14T15:58:27.047 回答