如何使用MVC DropDownlist

时间:2012-06-17 15:25:46

标签: asp.net-mvc viewmodel

我在MVC中遇到DropDownlist问题 我在我的应用程序中使用ModelView,这是我的代码

namespace MedicallexiconProject.ViewModel
{
    public class WordViewModel
    {
        private readonly ICategoryService _categoryService;
        public WordViewModel(ICategoryService categoryService)
        {
            _categoryService = categoryService;
            var selectList = _categoryService.GetAllCategorysSelectList().
                Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value = x.ID.ToString()
                }).ToList();
            Categories = selectList;
        }

        public WordViewModel()
        {

        }
        public string Name { get; set; }
        private IList<SelectListItem> _categories;
        public IList<SelectListItem> Categories
        {
            get
            {
                if (_categories == null)
                {
                    _categories = new List<SelectListItem>();
                }
                return (_categories);
            }

            set { _categories = value; }
        }
    }
}

这是我的控制器

[HttpGet]
public ActionResult Create()
{
    var wordViewModel = new WordViewModel(_categoryService);
    ViewBag.CategoryID = wordViewModel.Categories;
    return View();
}

[HttpPost]
public ActionResult Create(WordViewModel wordViewModel)
{
    Mapper.CreateMap<WordViewModel, Word>();

    var word = new Word();
    Mapper.Map(wordViewModel, word);

    if (ModelState.IsValid)
    {
        _wordService.AddNewWord(word);
        _uow.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(wordViewModel);
}

现在我如何在我的视图中插入下拉列表?

2 个答案:

答案 0 :(得分:3)

正如AlfalfaStrange所提到的,你不应该在ViewModel中添加逻辑。这让它变得丑陋!保持ViewModel简单的POCO。

在ViewModel中再添加一个名为“SelectedCategoryID”的属性,如下所示

public class WordViewModel
{
  public int SelectedCategoryID { set;get;}
  public IList<SelectListItem> Categories { set;get;}
  public string Name { set;get;}
}

ViewModel方法中初始化GET的商品(类别)。在这里,我调用一个名为GetCategories的方法,它返回一个类别列表。我可以随时随地调用该方法。

public ActionResult Create()
{
   var model=new WordViewModel();
   model.Categories=YourService.GetCategories();
   return View(model);
}

在强类型的“创建”视图中,使用此

@model WordViewModel
using(@Html.BeginForm())
{

  @Html.DropDownFor(x=>x.SelectedCategoryID,
               new SelectList(Model.Categories,"Value","Text"),"Select Category")
  <input type="submit" value="Save" />
}

HttpPost操作方法中,您可以检查所选值的wordViewModel.SelectedCategoryID

[HttpPost]
public ActionResult Create(WordViewModel wordViewModel)
{
   if(ModelState.IsValid)
   {
     //Checck for wordViewModel.SelectedCategoryID here now
   }
   //some validation failed. Let's reload the category data again.
   wordViewModel.Categories=YourService.GetCategories();
   return View(wordViewModel);
}

答案 1 :(得分:0)

包含在视图模型中加载下拉列表的代码绝对没问题。选择列表和下拉列表都是“查看”项....它们与业务逻辑无关,您的控制器和模型不需要了解有关SelectLists或SelectListItems或DropDownList等的任何信息。