无法在razor视图中访问ViewModel类成员

时间:2014-05-28 00:56:51

标签: asp.net-mvc-4 c#-4.0

我无法在视图中访问我的班级成员。所有其他成员都可以访问。

以下成员“ListOfCategories”在智能感知或视图内的运行时不可用。

错误:没有“IEnumerable”类型的ViewData项具有“ListOfCategories”键

我的ViewModelClass

public class ArticleEditViewModel
    {
        public int ID { get; set; }

        [Required]
        public string Title { get; set; }       

        public List<Category> ListOfCategories = new List<Category>();
    }

我的剃刀视图

@model rrrr.ViewModels.Article.ArticleEditViewModel

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_LayoutSecure.cshtml";
}

<div>
    @Html.ActionLink("Back to Articles", "Index")
</div>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
        @Html.HiddenFor(model  => model.ID)
        <div class="tableHeader">
            <h2>Update Article</h2>
            </div>             
                <table id="RegistrationForm">                               
                    <tbody>                         
                        <tr>
                            <td>Title</td>
                            <td>
                                @Html.TextBoxFor(model => model.Title, new { @class = "registerinput" })                                    
                            </td>
                            <td class="leftWarning">
                                @Html.ValidationMessageFor(model => model.Title)
                            </td>
                        </tr>           

                        <tr>
                            <td>Category</td>
                            <td>
                                 @Html.DropDownList(model => model.ListOfCategories, Model.CategoryID)
                            </td>           
                            <td class="leftWarning">
                                @Html.ValidationMessageFor(model => model.CategoryID)
                            </td>                 
                        </tr>                                                                                                                                      
                        <tr>
                            <td></td>
                            <td>
                                <input type="submit" value="Update Article" class = "css_button_4"  />
                            </td>                           
                        </tr>                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                    </tbody>                       
                </table>
}

1 个答案:

答案 0 :(得分:1)

请改用DropdownListFor。代码将是这样的:

@Html.DropDownListFor(model => model.CategoryId, Model.ListOfCategories.Select(c => new SelectListItem { Text = c.CategoryName, Value = c.CategoryId }))