Eyal
Eyal

Reputation: 4763

Model property is empty

I am trying to move from webForms to Asp.net-MVC and have some problems. I am trying to figure why this is not working, I am getting this error: "Object reference not set to an instance of an object"

I have the class 'Pages':

namespace _2send.Model
{   
    public class Pages
    {        
        public string PageContent { get; set; }        
        public string PageName { get; set; }       
        public int LanguageId { get; set; }      
    }    
}

I am inserting the value to 'Pages.PageContent' property with this class:

namespace _2send.Model.Services
{
    public class PagesService : IPagesService
    {
        public void GetFooterlinksPage()
        {

            DB_utilities db_util = new DB_utilities();           
            SqlDataReader dr;

            Pages pages = new Pages();

            using (dr = db_util.procSelect("[Pages_GetPageData]"))
            {
                if (dr.HasRows)
                {
                    dr.Read();
                    pages.PageContent = (string)dr["PageContent"];
                    dr.Close();
                }                
            }

        }

The Controller method looks like this:

private IPagesService _pagesService;
    public FooterLinksPageController(IPagesService pagesService)
    {
        _pagesService = pagesService;
    }

public ActionResult GetFooterLinksPage()
        {            
            _pagesService.GetFooterlinksPage();
            return View();      
        }

I am trying to write the property in the view like this:

@model _2send.Model.Pages
<div>
    @Model.PageContent;
</div>

When debugging, the method is fired and the dataReader is inserting the value to the 'PageContent' property, but I am still getting this error from the view.

Thanks!

Upvotes: 0

Views: 288

Answers (3)

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

You need to rewrite service method to return Pages:

    public Pages GetFooterlinksPage()
    {

        DB_utilities db_util = new DB_utilities();           

        Pages pages = new Pages();

        using (var dr = db_util.procSelect("[Pages_GetPageData]"))
        {
            if (dr.HasRows)
            {
                dr.Read();
                pages.PageContent = (string)dr["PageContent"];                    
                return pages;
                // Because you use using, you don't need to close datareader
            }                
        }
    }

And then rewrite your action method:

    public ActionResult GetFooterLinksPage()
    {            
        var viewmodel = _pagesService.GetFooterlinksPage();
        return View(viewmodel);      
    }

Upvotes: 1

MuriloKunze
MuriloKunze

Reputation: 15583

You can return a model:

var viewmodel = new _2send.Model.Pages().
//here you configure your properties

return View(viewmodel);

Upvotes: 1

SLaks
SLaks

Reputation: 887453

return View();

You didn't pass a model.

You need to pass the model as a parameter to the View() method.

Upvotes: 5

Related Questions