5

I have one of these annoying issues which are very simple and still I can't seem to find the problem.

I have a simple 'test' controller. here is the code:

public class testController : Controller
{
    //
    // GET: /test/

    public ActionResult Index(int id)
    {
        ItemModel item = null;
        item = CommonLib.ReturnFullItem(id);

        return View("~/Views/Emails/ItemRecordedEmail", item);
    }

}

and I do have the folder /Views/Emails/ with ItemRecordedEmail.cshtml view inside. But I get the following error message:

The view '/Views/Emails/ItemRecordedEmail' or its master was not found or no view engine supports the searched locations. The following locations were searched: /Views/Emails/ItemRecordedEmail

when I move the ItemRecordedEmail.cshtml to some shared folder and change the code accordingly everything seems to work fine. Any ideas what do I do wrong?

EDIT: I changed the path to '~/Views/Emails. This was the original path, I accidently left it out after all the tries I made. Still the same problem with the same error message, only this time it looks for it in the '~/Views/Emails' folder.

8
  • 2
    just try with return View("~/Views/Emails/ItemRecordedEmail", item); Commented Feb 14, 2013 at 8:26
  • @user739809 check my answer and also please try to put full error from browser in the question next time. Commented Feb 14, 2013 at 8:27
  • Instead of returning View, I would do the redirect to action. I assume that Emails is a controller name. Commented Feb 14, 2013 at 8:31
  • 2
    @Floradu88 To create cleaner solution. Later in the project something will change in the ItemRecordedEmail view that needs data transofrmed in action and there will be a problem. Commented Feb 14, 2013 at 8:48
  • 1
    return View("~/Views/Emails/ItemRecordedEmail.cshtml", item); Commented Feb 14, 2013 at 9:34

4 Answers 4

14

When using the tilde syntax to provide complete path to your view, you must supply the file extension of the view because this bypasses the view engine’s internal lookup mechanism for finding views. So it should be

return View("~/Views/Emails/ItemRecordedEmail.cshtml", item);

Reference : Professional ASP.NET MVC 4

Sign up to request clarification or add additional context in comments.

3 Comments

provide msdn reference for this please for a complete answer.
Thanks, searched for MSDN reference but could not find it. However, it is mentioned in Scott Hanselman's ASP.NET MVC book.
Thanks a lot!!! stupid from me not to check such a simple matter. Anyway, this works, of course. Thanks again to all.
6

For the Googler's there's another possible fix tucked away here:

https://stackoverflow.com/a/31345035/591097

Make sure the View file's properties Build Action is set to Content and not None.

Comments

3

I think you meant to use this code:

public ActionResult Index(int id)
    {
        ItemModel item = null;
        item = CommonLib.ReturnFullItem(id);

        return View("ItemRecordedEmail", item);
    }

or this one:

public ActionResult Index(int id)
    {
        ItemModel item = null;
        item = CommonLib.ReturnFullItem(id);

        return View("~/Views/Emails/ItemRecordedEmail.cshtml", item);
    }

You need to relatively add a view to path and it will search in area/test/view.html then to area/shared/view.html then to the rest of the path without area. Like the paths presented in the error message you got with this error message.

Edit: if you have a view the use this

return View("~/Views/Emails/ItemRecordedEmail.cshtml", item);

or if you have a partial view:

return Partial("~/Views/Emails/ItemRecordedEmail.cshtml", item);

Comments

0

I got this problem when I copied and renamed an existing view. MVC couldn't find the new view. I solved it by first deleting the copied and renamed file, then adding a completely new view and copy pasting in the content from the existing view.

Comments

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.