[RESOLVED]Returning a Model Data ToList() to a differently named View

Using MVC 5.2.2

Inside a CategoryController controllerr, I have this method:

        public ActionResult DisplayCategory(String CategoryDesc)
        {
                return View(db.Messages.ToList());
        }

I want to return the model data to a view named "index.cshtml" because I don’t have a view called DisplayCategory.   

If I run this, I get this error as it is looking for the DisplayCategory view by default.

The view ‘DisplayCategory’ or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Category/DisplayCategory.aspx
~/Views/Category/DisplayCategory.ascx
~/Views/Shared/DisplayCategory.aspx
~/Views/Shared/DisplayCategory.ascx
~/Views/Category/DisplayCategory.cshtml
~/Views/Category/DisplayCategory.vbhtml
~/Views/Shared/DisplayCategory.cshtml
~/Views/Shared/DisplayCategory.vbhtml

If I change this method to:

        public ActionResult DisplayCategory(String CategoryDesc)
        {
                return View("Index");
        }

I get this error:

Object reference not set to an instance of an object.

So I’m slightly confused on how I return the data to the index view and specify that it is the index view that I want to show…

Hi there,

Try return view ("Index", db.Messages.ToList());

Thanks!

Hi iforsythCP,

Thanks for your post.

iforsythCP

I want to return the model data to a view named "index.cshtml" because I don’t have a view called DisplayCategory.

You can’t return view in DisplayCategory action,because you didn’t have
DisplayCategory view.You can try this:

public ActionResult DisplayCategory(String CategoryDesc)
        {
          // //Do coding staff.

                return RedirectToAction("index");
        }

Hope this can be helpful.

Best Regards,

Eileen

Leave a Reply