Karen Slon
Karen Slon

Reputation: 233

Go to another view on button click - view is not found

"Test.cshtml" view is never found.

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

Among listed locations there are:

but not correct location which should be "~/Views/SecondGroup/Test/123" ??

ViewOne.cshtml (generated by FirstController)

<button onclick= "@("window.location.href ='" + @Url.Action("Test", 
"SecondController", new { id = "123" }) + "'");">
 GO TO ANOTHER VIEW
</button>

SecondController

public ActionResult Test(string id)
{           
    return View("Test", id);  // here id = '123'          
}

Test.cshtml

@model String
@{    
   Layout = "~/Views/Shared/_Layout.cshtml";
}
<div> .... </div>

Here is the folder structure:

---Controllers
     --- HomeController
     --- FirstController
     --- SecondController

-- Views
     --- FirstGroup
         --- ViewOne

     --- SecondGroup
         --- Test

Why "Test.cshtml" view is never found?

Thank you

Upvotes: 0

Views: 297

Answers (1)

user3559349
user3559349

Reputation:

In your Test(string id) method, you're using return View("Test", id); which uses this overload because the type of id is a string.

In that overload, the 2nd parameter is the name of the master page or template to use when the view is rendered.

You need to use this overload where the 2nd parameter is object (your model).

Change the code to cast the string to object:

public ActionResult Test(string id)
{           
    return View("Test", (object)id);       
}

Upvotes: 3

Related Questions