Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:34:38+00:00 2026-06-11T11:34:38+00:00

Possible Duplicate: MVC3 Razor (Drop Down List) I’ve been getting Object reference not set

  • 0

Possible Duplicate:
MVC3 Razor (Drop Down List)

I’ve been getting “Object reference not set to an instance of an object.”. Been trying to figure out this error for the past 12 hours. Hope someone can help.

This is causing the error.

@Html.DropDownListFor(c => c.CategoryID, Model.CategoryTypeList)

In SearchController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;

namespace Demo.Controllers
{
public class SearchController : Controller
{
    //
    // GET: /Search/

    public ActionResult DisplayCategory()
    {
        var model = new SearchModel();
        model.CategoryTypeList = GetCategory();

        return View(model);
    }


    private List<SelectListItem> GetCategory()
    {
        List<SelectListItem> items = new List<SelectListItem>();
        items.Add(new SelectListItem { Text = "1", Value = "1" });
        items.Add(new SelectListItem { Text = "2", Value = "2" });
        return items;
    }

}
}

In SearchModel

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Demo.Controllers;

namespace Demo.Models
{
public class SearchModel
{
    public List<SelectListItem> CategoryTypeList { get; set; }

    [Display(Name = "Category")]
    public string CategoryID { get; set; }

}
}

In CSHTML

@model Demo.Models.SearchModel
@{
    ViewBag.Title = "Search";
}

<h2>Search</h2>
@using (Html.BeginForm())
{
<table>
<tr>
<td>@Html.LabelFor(c => c.CategoryID)</td>
<td>@Html.DropDownListFor(c => c.CategoryID, Model.CategoryTypeList)</td>
</tr>

</table>
}

Stack Trace

[NullReferenceException: Object reference not set to an instance of an object.]
ASP._Page_Views_Home_Search_cshtml.Execute() in c:\Users\User_me\Documents\Visual Studio 2010\Projects\Demo\Demo\Views\Home\Search.cshtml:12
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +272
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +67
System.Web.WebPages.StartPage.RunPage() +58
System.Web.WebPages.StartPage.ExecutePageHierarchy() +94
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +172
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +574
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +360
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +409
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +60
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +391
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +61
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +285
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830
System.Web.Mvc.Controller.ExecuteCore() +136
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +232
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +68
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +61
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +31
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +56
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +110
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:34:39+00:00Added an answer on June 11, 2026 at 11:34 am

    Per your comments above, if you want to call this as a seperate unit within another page, then you will need to either pass the model in using Html.RenderPartial, or call Html.RenderAction in order to execute the entire action and output the resulting HTML.

    Using Html.RenderPartial

    @*
       Assuming that you have already initialized some variable
       called 'mySearchModel'
    *@
    
    @Html.RenderPartial("DisplayCategory", mySearchModel)
    

    This will ensure that the Model property on the view gets set.

    Using Html.RenderAction

    //Slight change to your action method to ensure it returns
    // a partial view, and will only ever be called as a child
    // action of another action.
    [ChildActionOnly]
    public ActionResult DisplayCategory()
    {
        var model = new SearchModel();
        model.CategoryTypeList = GetCategory();
    
        return PartialView(model);
    }
    
    
    @Html.RenderAction("DisplayCategory")
    

    Further reading:

    http://haacked.com/archive/2009/11/17/aspnetmvc2-render-action.aspx
    http://devlicio.us/blogs/derik_whittaker/archive/2008/11/24/renderpartial-vs-renderaction.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: MVC3 custom validation: compare two dates I am trying to make a
Possible Duplicate: ASP.NET MVC3 how to excute action method of controller using timer with
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Emitting unencoded strings in a Razor view I have a string that
Possible Duplicate: After a computer crash my Visual Studio 2010 will not load an
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Session variable is lost on RedirectToAction in IE I'm using MVC3 and
Possible Duplicate: Schedule Controls for ASP.Net MVC I have MVC3 in which I want
Possible Duplicate: Ioc/DI - Why do I have to reference all layers/assemblies in entry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.