0

可能重複:
MVC3 Razor (Drop Down List)MVC3下拉列表

我已經得到 「未將對象引用設置到對象的實例。」一直試圖找出過去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 
+0

哪行引發錯誤? – McGarnagle

+1

事實上,如果您將代碼複製並粘貼到空白MVC解決方案中,您轉換完全相同的問題並不會改變您的代碼正常工作的事實。除了您發佈的內容之外,還有其他內容正在發佈,我們將需要比您提供的信息更多的信息。異常的堆棧跟蹤將是一個很好的開始。 – Josh

+0

這是如何被稱爲?通過AJAX請求,直接作爲URL?作爲另一個視圖中的部分呈現? – Josh

回答

0

根據以上您的意見,如果您想將其作爲另一個頁面內的獨立單元調用,那麼您需要使用Html.RenderPartial傳遞模型,或者調用Html.RenderAction以執行整個操作並輸出生成的HTML。

使用Html.RenderPartial

@* 
    Assuming that you have already initialized some variable 
    called 'mySearchModel' 
*@ 

@Html.RenderPartial("DisplayCategory", mySearchModel) 

這將確保在視圖模型屬性,都會設置。

使用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") 

延伸閱讀:

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