C#MVC 3使用选择列表,在视图中选择价值 [英] C# mvc 3 using selectlist with selected value in view

查看:94
本文介绍了C#MVC 3使用选择列表,在视图中选择价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个MVC3 Web应用程序。我想从编辑应用程序磨片管理层系统中的一个BLO时所显示的类别列表。在我的视图模型我得为类别selectlistitems的列表中定义以下属性。

  ///<总结>
///类别列表
///< /总结>
[显示(NAME =Categorie)]
公共IEnumerable的< SelectListItem>分类{搞定;组; }

下一步,我的控制器包含在那里selectlistitems的列表是从数据库中充满以下编辑操作。

 公众的ActionResult编辑(GUID ID)
{
    变种blogToEdit = _blogService.First(X => x.Id.Equals(ID));
    变种listOfCategories = _categorieService.GetAll();
    VAR选择列表= listOfCategories.Select(X =>新建SelectListItem的{text = x.Name,值= x.Id.ToString(),选择= x.Id.Equals(blogToEdit.Category.Id)})了ToList() ;
    selectList.Insert(0,新SelectListItem的{text = Messages.SelectAnItem,值= Messages.SelectAnItem});    VAR视图模型=新BlogModel
                        {
                            BlogId = blogToEdit.Id,
                            活动= blogToEdit.Actief,
                            内容= blogToEdit.Text,
                            标题= blogToEdit.Titel,
                            分类=选择列表//在这一点上,我看到被选中预期的项目
                            //分类=新的IEnumerable< SelectListItem>(listOfCategories,ID,纳摩,blogToEdit.CategorieId)
                        };
    返回视图(视图模型);
}

当我设置被返回视图之前断点,我认为这是我所期望的选择列表被填满。所以在这一点上似乎一切都还好。该视图模型充满完全正确的。
然后,在我看来(我用剃刀)我已经得到了,这是为了使选择列表对我来说,以下两条规则。

  @ Html.LabelFor(M = GT; m.Categories)@ Html.DropDownListFor(型号=> model.Categories,Model.Categories,Model.CategoryId)
@ Html.ValidationMessageFor(M = GT; m.Categories)

当我运行code和打开视图编辑我的博客,我可以看到所有正确的数据。另外,选择列表中正确呈现,但我想选择的项目失去了它的选择。这怎么可能?直到该点的视图模型被这样的观点一切正常返回。但是,当我在浏览器中查看网页时,选择列表有只出正确的选择。我缺少的是在这里吗?还是做错了?


解决方案

  @ Html.DropDownListFor(型号=> model.Categories,Model.Categories,Model.CategoryId)

在这里,你没有正确使用辅助方法。第一个参数必须是在你的视图模型的属性将包含当前选定的数值。它应该是一个标量属性,而不是一个集合。

因此​​,在您的视图模型,你需要添加这样的属性:

  [显示(NAME =Categorie)]
公共IEnumerable的< SelectListItem>分类{搞定;组; }
公共字符串的SelectedValue {搞定;组; }

而在你的控制器动作:

  VAR选择列表= listOfCategories.Select(X =>新建SelectListItem {
    文= x.Name,
    值= x.Id.ToString()
})了ToList()。VAR视图模型=新BlogModel
{
    BlogId = blogToEdit.Id,
    活动= blogToEdit.Actief,
    内容= blogToEdit.Text,
    标题= blogToEdit.Titel,
    类别=选择列表,
    //这是设置所选值
    的SelectedValue = blogToEdit.Category.Id
};

和在你看来简单:

  @ Html.DropDownListFor(X => x.SelectedValue,Model.Categories)

I'm working on a MVC3 web application. I want a list of categories shown when editing a blo from whe applications managements system. In my viewmodel i've got the following property defined for a list of selectlistitems for categories.

/// <summary>
/// The List of categories
/// </summary>
[Display(Name = "Categorie")]
public IEnumerable<SelectListItem> Categories { get; set; }

The next step, my controller contains the following edit action where the list of selectlistitems is filled from the database.

public ActionResult Edit(Guid id)
{
    var blogToEdit = _blogService.First(x => x.Id.Equals(id));
    var listOfCategories = _categorieService.GetAll();
    var selectList = listOfCategories.Select(x =>new SelectListItem{Text = x.Name, Value = x.Id.ToString(), Selected = x.Id.Equals(blogToEdit.Category.Id)}).ToList();
    selectList.Insert(0, new SelectListItem{Text = Messages.SelectAnItem, Value = Messages.SelectAnItem});

    var viewModel = new BlogModel
                        {
                            BlogId = blogToEdit.Id,
                            Active = blogToEdit.Actief,
                            Content = blogToEdit.Text,
                            Title = blogToEdit.Titel,
                            Categories = selectList //at this point i see the expected item being selected
                            //Categories = new IEnumerable<SelectListItem>(listOfCategories, "Id", "Naam", blogToEdit.CategorieId)
                        };
    return View(viewModel);
}

When i set a breakpoint just before the view is being returned, i see that the selectlist is filled as i expected. So at this point everything seems to be okay. The viewmodel is filled entirely correct. Then in my view (i'm using Razor) i've got the following two rules which are supposed to render the selectlist for me.

@Html.LabelFor(m => m.Categories) @Html.DropDownListFor(model=>model.Categories, Model.Categories, Model.CategoryId)
@Html.ValidationMessageFor(m => m.Categories)

When I run the code and open the view to edit my blog, I can see all the correct data. Also the selectlist is rendered correctly, but the item i want to be selected lost it's selection. How can this be? Until the point the viewmodel is being returned with the view everything is okay. But when i view the webpage in the browser, the selectlist is there only with out the correct selection. What am I missing here? Or doing wrong?

解决方案

@Html.DropDownListFor(model=>model.Categories, Model.Categories, Model.CategoryId)

Here you are not properly using the helper method. The first argument must be a property on your view model which will contain the currently selected value. It should be a scalar property, not a collection.

So in your view model you need to add such property:

[Display(Name = "Categorie")]
public IEnumerable<SelectListItem> Categories { get; set; }
public string SelectedValue { get; set; }

And in your controller action:

var selectList = listOfCategories.Select(x => new SelectListItem {
    Text = x.Name, 
    Value = x.Id.ToString() 
}).ToList();

var viewModel = new BlogModel
{
    BlogId = blogToEdit.Id,
    Active = blogToEdit.Actief,
    Content = blogToEdit.Text,
    Title = blogToEdit.Titel,
    Categories = selectList,
    // this is what sets the selected value
    SelectedValue = blogToEdit.Category.Id 
};

And in your view simply:

@Html.DropDownListFor(x => x.SelectedValue, Model.Categories)

这篇关于C#MVC 3使用选择列表,在视图中选择价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆