Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC The view 'name' or its master was not found

Tags:

asp.net-mvc

I have a problem with a View. Here's the code snippet:

    public ActionResult AddAdvertisement()
    {
        ...
        return RedirectToAction("AdvCreated");
    }

    [HttpGet]
    public ActionResult AdvCreated()
    {
        return View("AdvCreated", "abc");
    }

then I see the error

The view 'AdvCreated' or its master was not found. The following locations were searched:

~/Views/Advertisement/abc.master

~/Views/Shared/abc.master

If I just go to URL http://localhost/AdvCreated everything is OK. Why ?

like image 284
Tony Avatar asked Dec 09 '22 12:12

Tony


2 Answers

What I understand is you are trying to pass a string to View as model. It's not possible. There is an overload of View function like this:

View(string viewName,string masterViewName)

So it looks for a Master View named "abc". If you want to pass a string, convert it to an object.There is an example here.

like image 102
Ufuk Hacıoğulları Avatar answered Dec 29 '22 03:12

Ufuk Hacıoğulları


You need to do the following

return View("AdvCreated", (object)"abc");

Or if you are using .NET 4 you can even do this:

return View("AdvCreated", model: "abc");

This forces the Framework to use the correct overloadthat treats the second parameter as the model.

like image 31
marcind Avatar answered Dec 29 '22 02:12

marcind