0

重複の可能性:
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;
    }

}
}

In Searchモデル

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
4

1 に答える 1

0

上記のコメントによると、これを別のページ内の別のユニットとして呼び出す場合は、Html.RenderPartial を使用してモデルを渡すか、アクション全体を実行して結果を出力するために Html.RenderAction を呼び出す必要があります。 HTML。

Html.RenderPartial の使用

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

@Html.RenderPartial("DisplayCategory", mySearchModel)

これにより、ビューの Model プロパティが確実に設定されます。

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

于 2012-09-16T00:34:10.673 に答える