English 中文(简体)
ASP.Net MVC 2 DropDownListFor in EditorTemplate
原标题:

I have a view model that looks like this:

namespace AutoForm.Models
{
    public class ProductViewModel
    {
        [UIHint("DropDownList")]
        public String Category { get; set; }

        [ScaffoldColumn(false)]
        public IEnumerable<SelectListItem> CategoryList { get; set; }

        ...
    }
}

It has Category and CategoryList properties. The CategoryList is the source data for the Category dropdown UI element.

I have an EditorTemplate that looks like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ProductViewModel>" %>
<%@ Import Namespace="AutoForm.Models"%>

<%=Html.DropDownListFor(m => m.Category , Model.CategoryList ) %>

NOTE: this EditorTemplate is strongly typed to ProductViewModel

My Controller is populating CategoryList property with data from a database.

I cannot get the DropDownListFor template to render a drop down list with data from CategoryList. I know CategoryList is getting populated with data in the controller because I see the data when I debug and step through the controller.

Here s my error message in the browser:

Server Error in / Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 2: <%@ Import Namespace="AutoForm.Models"%> Line 3: Line 4: <%=Html.DropDownListFor(m => m.Category, Model.CategoryList) %>

Source File: c:ProjectStoreAutoFormAutoFormViewsSharedEditorTemplatesDropDownList.ascx Line: 4

Any ideas?

Thanks

Tom


As a followup, I noticed that ViewData.Model is null when I m stepping through the code in the EditorTemplate. I have the EditorTemplate strongly typed to "ProductViewModel" which is also the type that s passed to the View in the controller. I m perplexed as to why ViewData.Model is null even though it s getting populated in the controller before getting passed to the view.

问题回答
<%=Html.DropDownListFor(m => m.Category , Model.CategoryList ) %>

If your model is null then that call to Model.CategoryList will not work. You can use a static function instead.





相关问题
ASP.Net MVC - Handle Multiple Checkboxes

Ok, I have a role based permission system in place and would like admin s to be able to edit the permissions for each role. To do this I need to load lots of checkboxes, however I m struggling with ...

Entity Framework error when submitting empty fields

VS 2010 Beta 2, .NET 4. In my ASP.NET MVC 2 application, when I submit a form to an action method that accepts an object created by the entity framework, I get the following error: Exception Details:...

ASP.Net MVC Architecture - Location of ViewModels

We have a decent sized MVC project running well at the moment, i ve started to look at some re-factoring and I have a question. At the moment the Data Layer and Service Layer is stored in a seperate ...

ASP.Net MVC Go To Declaration On view

Is there anyway when in an MVC view to right click a function call and get to the declaration? Like you can do in the code behind/controllers

ASP.NET MVC 2 Beta - Default Model Binder

I m experiencing some different behavior after switching from ASP.NET MVC 1.0 to ASP.NET MVC 2 Beta. I checked the breaking changes but it s not clear where the issue lies. The problem has to do with ...

Asp.net MVC Dynamic Menu System

In our current Asp.net MVC application we have 2 menu systems one accross the top and one on the left hand side. Now we have a partial view that renders the menu, however is the only way for this too ...

热门标签