C# mvc 3 在视图中使用带有选定值的选择列表 [英] C# mvc 3 using selectlist with selected value in view

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

问题描述

我正在开发 MVC3 Web 应用程序.我想要在从应用程序管理系统编辑 blo 时显示的类别列表.在我的视图模型中,我为类别的选择列表项列表定义了以下属性.

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; }

下一步,我的控制器包含以下编辑操作,其中从数据库填充 selectlistitems 列表.

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);
}

当我在返回视图之前设置断点时,我看到选择列表已按预期填充.所以在这一点上,一切似乎都很好.视图模型填充完全正确.然后在我看来(我正在使用 Razor)我有以下两条规则,它们应该为我呈现选择列表.

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 
};

而且在您看来很简单:

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

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

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