可能重复:
MVC3 Razor(下拉列表)
我一直在收到“对象引用未设置为对象的实例。”。在过去的 12 小时内一直试图找出这个错误。希望有人可以提供帮助。
这导致了错误。
@Html.DropDownListFor(c => c.CategoryID, Model.CategoryTypeList)
在 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;
}
}
}
在 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; }
}
}
在 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>
}
堆栈跟踪
[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