ASP.NET MVC Name "name" or its master was not found

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

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

then I see an error

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

~ / Views / Advertising / abc.master

~ / Views / Shared / abc.master

If I just go to the URL http: // localhost / AdvCreated , everything will be fine. Why?

+4
source share
3 answers

I understand that you are trying to pass a string to represent as a model. It's impossible. There is an overload of the View function as follows:

 View(string viewName,string masterViewName) 

So he is looking for a master view named "abc". If you want to pass a string, convert it to an object. Here is an example.

+8
source

You need to do the following

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

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

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

This forces the Framework to use the correct overload, which treats the second parameter as a model.

+5
source

Your / aspx / ascx view should be located inside one of the folders you listed, such as the code in your controller.

If you just do this:

 return RedirectToAction("AdvCreated"); 

ASP.NET MVC will assume that you have view / ascx / aspx located in your controller’s folder - in your case ~ / Views / Advertisement / folder or a shared folder.

If you have a specific view to display outside the intended folders, you can specify this directly, for example:

 return RedirectToAction("~/MyFolder/AdvCreated.ascx"); 
+1
source

All Articles