1

I want to edit blogs. I have Edit view in View folder in BlogAdmin folder.

 <a  href="/BlogAdmin/Edit/@item.id" class=" btn btn-sm btn-primary float-right"> Edit </a>  

Controller:

  public ActionResult Edit(int? id)
         {
        if (id == null)
        {
            return HttpNotFound();
        }
        List<Blog> EditBg = db.Blogs.Where(i => i.id==id).ToList();
        if (EditBg== null)
        {
            return HttpNotFound();
        }
        var vm = new VmBlog
        {
            blogcatg = db.BlogCategories.ToList(),
            Blog = db.Blogs.Where(i => i.id == id).ToList(),
            Content=db.Blogs.FirstOrDefault(i=>i.id==id).posttext

        };

        return View(vm);

    }

Get method is working. There is no error here: enter image description here

Everything seems correctly. But I am getting this error:

The view 'Edit' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/BlogAdmin/Edit.aspx

~/Views/BlogAdmin/Edit.ascx

~/Views/Shared/Edit.aspx

~/Views/Shared/Edit.ascx

~/Views/BlogAdmin/Edit.cshtml

~/Views/BlogAdmin/Edit.vbhtml

~/Views/Shared/Edit.cshtml

~/Views/Shared/Edit.vbhtml

Why does error happen?

Aytac
  • 305
  • 2
  • 19

2 Answers2

1

Check Microsoft documentation on Views, specifically the section about View Discovery

It doesn't matter if you implicitly return the ViewResult with return View(); or explicitly pass the view name to the View method with return View("");. In both cases, view discovery searches for a matching view file in this order:

Views/[ControllerName]/[ViewName].cshtml

Views/Shared/[ViewName].cshtml

It seems that your folder structure does not account for the specified view discovery

If you want to change the folder structure and View discovery

You can customize the default convention for how views are located within the app by using a custom IViewLocationExpander.

A very nice answer on this, can be found Working with IViewLocationExpander in mvc

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
1

@Aytac,

In the MVC pattern the default routing puts controllers into a folder at "/controllers". Views are stored in a folder at "/Views/{ControllerBaseName}/{ActionName}".

The default folder structure (routing) should look like the following (tagged as c# so assuming c# extensions):

/Controllers/BlogAdminController.cs

/Views/BlogAdmin/Edit.cshtml

George N
  • 144
  • 5
  • I think I did not ask question properly. I have Edit view in View folder in BlogAdmin folder. When i changed ` Edit ` to ` Edit ` it finds edit view. – Aytac Jun 01 '20 at 13:10
  • Would you check the generated HTML of the link of your view in your browser? What does it contain? Is @item.id being correctly replaced with a number? – George N Jun 01 '20 at 13:20
  • https://localhost:44348/BlogAdmin/Edit/7 is with this code ` Edit ` . And https://localhost:44348/HMadeAdmin/BlogAdmin/Edit/7 is with the razor syntax. – Aytac Jun 01 '20 at 13:50