ListBoxFor没有约束力我的视图模型 [英] ListBoxFor not binding my viewmodel

查看:144
本文介绍了ListBoxFor没有约束力我的视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道那朵问题已经被问很配发的SO。

<一个href=\"http://stackoverflow.com/questions/3194143/challenges-with-selecting-values-in-listboxfor\">here

<一href=\"http://stackoverflow.com/questions/2308846/why-is-listboxfor-not-selecting-items-but-listbox-is\">here

但我仍然无法找出问题。

我开发一个博客来教自己的MVC框架。现在,当我后下认为,ListBoxFor助手没有任何值绑定到我的模型。该列表包含了成功的所有类别但当POST控制器回来类别对象视图模型为空。

下面是视图模型:

 公共类PostViewModel
{
    公开发表文章{搞定;组; }
    公共IEnumerable的&LT;类别及GT;分类{搞定;组; }
}

控制器:

 公众的ActionResult的Create()
    {
        PostViewModel后=新PostViewModel();
        post.Categories = db.ListCategories();
        返回查看(后);
    }

视图:

 &LT; P&GT; @ Html.ListBoxFor(型号=&GT; model.Categories,新MultiSelectList(Model.Categories,类别ID,类别名称))&LT; / P&GT;


解决方案

我相信你应该在你的视图模型所选择的ID将绑定到一个数组属性。

 公共类PostViewModel
{
    公开发表文章{搞定;组; }
    公众诠释[] {SelectedCategoryIds获得;组; }
    公共IEnumerable的&LT;类别及GT;分类{搞定;组; }
}

和改变你的 Html.ListBoxFor 呼吁要为 SelectedCategoryIds 属性。

 &LT; P&GT; @ Html.ListBoxFor(型号=&GT; model.SelectedCategoryIds,新MultiSelectList(Model.Categories,类别ID,类别名称))&LT; / P&GT;


顺便说一句:现在你正在创建的 SelectedCategoryIds 属性列表框,如果你有列表中的标签,你应该改变这是为 SelectedCategoryIds 属性了。

  @ Html.LabelFor(型号=&GT; model.SelectedCategoryIds,类别)

类别为标签文本)

I know tis question has been asked quite allot on SO.

here and here

But I still can't figure out the problem.

I am developing a blog to teach myself the MVC framework. Now when I post the view below, The ListBoxFor helper does not bind any values to my model. The list successfully contains all the categories but when the POST controller gets back the view model the Categories object is null.

Here is the View Model:

public class PostViewModel
{
    public Post Posts { get; set; }
    public IEnumerable<Category> Categories { get; set; }
}

The Controller:

    public ActionResult Create()
    {
        PostViewModel post = new PostViewModel();
        post.Categories = db.ListCategories();
        return View(post);
    }

The View:

<p>@Html.ListBoxFor(model => model.Categories, new MultiSelectList(Model.Categories, "CategoryID", "CategoryName"))</p>

解决方案

I believe you should have an array property in your view model which the selected IDs will bind to.

public class PostViewModel
{
    public Post Posts { get; set; }
    public int[] SelectedCategoryIds { get; set; }
    public IEnumerable<Category> Categories { get; set; }
}

And change your Html.ListBoxFor call to be for the SelectedCategoryIds property.

<p>@Html.ListBoxFor(model => model.SelectedCategoryIds, new MultiSelectList(Model.Categories, "CategoryID", "CategoryName"))</p>


As an aside: Now that you are creating a list box for the SelectedCategoryIds property, if you have a label for the list you should change this to be for the SelectedCategoryIds property too.

@Html.LabelFor(model => model.SelectedCategoryIds, "Categories")

("Categories" is the label text)

这篇关于ListBoxFor没有约束力我的视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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