4

Before starting, I've seen this asked here before and I've followed the answer and examples given here:

how to display image from path in asp.net mvc 4 and razor view

But when i do

<img src="@Url.Content(Model.ImagePath)" class="dker" alt="..." />

I get an error

Source Error

   [No relevant source lines]
   [NullReferenceException: Object reference not set to an instance of an object.]

In my model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NSProfileImages
{
    public class ProfileImages
    {
        public string ImagePath
        {
            get
            {
                return "~/Assets/Images/user_images/avatars/[email protected]";
            }
        }
    }
}

View:

@model NSProfileImages.ProfileImages

<img src="@Url.Content(Model.ImagePath)" class="dker" alt="..." />

If I do

<img src="~/Assets/Images/user_images/avatars/[email protected]" class="dker" alt="..." />

it will display the image normally and no errors.

2
  • Do you actually pass an instance of the model to the view in your controller? Commented Jul 23, 2014 at 16:54
  • Which object is null? How are you providing the model to the view? Commented Jul 23, 2014 at 16:54

3 Answers 3

4

I suspect that you forgot to supply an instance of the model to the view.

public ActionResult Index()
{
    // here we instantiate the type and supply it to the view. 
    ViewData.Model = new ProfileImages();
    return View();
}

Alternatively, you could supply the model instance via the View method like so:

 return View(new ProfileImages());

Note that in this case you could very well make the model property static which will remove the need to supply the view model altogether:

public class ProfileImages {
    public static string ImagePath {
        get {
            return "~/Assets/Images/user_images/avatars/[email protected]";
        }
    }
}
...
<img src="@Url.Content(NsProfileImages.ProfileImages.ImagePath)" 
     class="dker" alt="..." />
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, all the methods you shared did work, thank you. Now what would you recommend is the better way to do this?
@Johhan It depends really. But generally the first approach will be more flexible. Please consider marking my answer as accepted if it answered your question :)
0

In your controller you should have something like this:

 ActionResult Index(){
 return View(new ProfileImages());
 }

Comments

0

Maybe you are not passing model to view:

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

3 Comments

that did it, now I'm wondering, do I need to do the same for every view?
No. For posts, the model received will be automatically passed if none is specified.
But in your case, as the field is read-only, the value needs to be in a hiddenfield to kept between posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.