Akbari
Akbari

Reputation: 2409

How to pass Parameter to a partial view: Object reference not set to an instance of an object

I have a partial view that renders one control on the page, this control is for certain fields that are long. This is how I defined it:

@model long
@(Html.Kendo().MultiSelectFor(x => Model)

I'm trying to render this partial view in other views with something like:

@Html.Partial("MultiSelect/partialView", @Model.longField)

This give me an Object reference not set to an instance of an object exception.

Please guide me about the right syntax to achieve this.

Upvotes: 0

Views: 444

Answers (2)

Nitin Varpe
Nitin Varpe

Reputation: 10694

Check if @Model is null. If yes, try to create object of your model to pass to view. You cannot access property of null object as you have done.

 var model= new YourViewModel();
 return View(model);

Upvotes: 2

Balázs
Balázs

Reputation: 2939

I can think of two things being mistaken here, though it depends on your solution.

  1. Check your path to the partial view's name. You can only write MultiSelect/partialView if the MultiSelect folder and the partialView.cshtml are in the same folder as the view from which you are calling Html.Partial(...). That is, your folder structure is something like this:

    Views
     | YourViewFolder
         YourView.cshtml
         | MultiSelect folder
            partialView.cshtml
    

In other words, in your code, you specify a relative path to your current view. (Note that your partial view can also be in /Views/Shared, Razor will find it in that case as well.) If your fodler structure is not like that above (or is not in /Views/Shared/, then you must specify the path relative to your project like this: /Views/.../MultiSelect/partialView where ... is any directory in between Views and MultiSelect.

Other thing that you should check is that @(Html.Kendo().MultiSelectFor(x => Model) line is missing a right paren ) from the end of it, I am not sure if this is a copy-paste error or it is like that in your view.

Upvotes: 1

Related Questions