Answered by:
How to switch to "View"?

Question
-
User1134142769 posted
Question.
1. What HTML page elements to use to follow links to other View?
Example.
1.1 - <a> </a>
1.2 - <input type = "submit" name = "name" value = "submit_1" />
1.3 - <button> </button>
others?
2. What principles are used to switch to View?
What are the most effective principles?
What are the most preferred principles?
Example.
2.1 Follow direct links (href = "~ / Views / View1 /");
2.2 Transition using the controller method;
I understand correctly?
3. Ways to click on the links?
What are the most effective ways?
What are the most preferred methods?
Example.
3.1 - href;
3.2 - tag-helpers;
others?
4. Is it possible to place several `View` in the folder of one controller?
Example \ Home \ View0.cshtml;
Description.
I understand that the question is probably wide, so at least I will be grateful for a short answer to the question.
Please apply the answer to my project structure.
Browse Pages:
- \ Home \ View0.cshtml;
- \ View1 \ Index.cshtml.
Code (my attempts).HomeController.cs
using Microsoft.AspNetCore.Mvc; namespace WebAppCore.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult View1() { return View(); } public ActionResult View2() { return View(); } } }
\Home\Index.cshtml
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers <h1> *** Test View Main ***</h1> <h2> *** From the box ***</h2> <a href="~/Views/View1/">A_View1</a> <input type="submit" name="name" value="submit_1" /> <button>button_1</button> <h2> *** Tag hepler ***</h2> <a asp-action="View1">A_action</a> <a action="View1">A_action</a> <button asp-action="View1">button_1_1</button>
Saturday, April 4, 2020 1:25 PM
Answers
-
User711641945 posted
Hi frmasp897654,
1. What HTML page elements to use to follow links to other View?All of the html elements could be used to link to the other view.
Example:
View
<a asp-controller="Home" asp-action="View0">View0</a> <a asp-controller="View1" asp-action="Index">View1/Index</a>
<form asp-controller="Home" asp-action="View0"> <button>View0</button> </form> <form asp-controller="View1" asp-action="Index"> <button>View1/Index</button> </form>
<form asp-controller="Home" asp-action="View0"> <input type="submit" value="View0" /> </form> <form asp-controller="View1" asp-action="Index"> <input type="submit" value="View1/Index" /> </form>Controller
public class HomeController : Controller { public IActionResult Index() { return View();//display the Home/Index.cshtml } public IActionResult View0() { return View();//display the Home/View0.cshtml } }
public class View1Controller : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();//display View1/Index.cshtml
}
}2. What principles are used to switch to View?The fastest way is to use href="url".
3. Ways to click on the links?It depends on your scenario.
4. Is it possible to place several `View` in the folder of one controller?Did you mean you want to put several views in the Views/Home folder and you could render the view by the actions in the HomeController?If so,of course you can.
public class HomeController : Controller { public ActionResult Index() { return View();//display the Home/Index.cshtml } public ActionResult View1() { return View();//display the Home/View1.cshtml } }
Reference:
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-3.1#view-discovery
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 6, 2020 8:11 AM
All replies
-
User475983607 posted
You misunderstand MVC fundamentals. Views are returned by Actions. Views are never called directly. I recommend going through the getting started tutorials before moving forward.
Saturday, April 4, 2020 4:13 PM -
User1134142769 posted
You misunderstand MVC fundamentals. Views are returned by Actions. Views are never called directly. I recommend going through the getting started tutorials before moving forward.
I have long familiarized myself with the initial lessons.
But in practice, something does not work ...To open the window "View1" I use.
Method 1. asp-controller = "Home" asp-action = "View1".
Code ".. \ Home \ Index.cshtml"
Works -<a asp-controller="Home" asp-action="View1"> View1 </a>
Doesn't work -<button asp-controller = "Home" asp-action = "View1"> button_1_1 </button>
Doesn't work -<input type = "submit" name = "name" asp-controller = "Home" asp-action = "View1" value = "submit_1" />
Code "HomeController.cs"
public ActionResult View1 ()
{
// return View ();
return View ("Views / View1 / Index.cshtml");
}
Method 2. asp-controller = "View1" asp-action = "Index"
Code ".. \ Home \ Index.cshtml"
Works -<a asp-controller="View1" asp-action="Index"> View1 </a>
Doesn't work -<button asp-controller = "View1" asp-action = "Index"> button_1_1 </button>
Doesn't work -<input type = "submit" name = "name" asp-controller = "View1" asp-action = "Index" value = "submit_1" />
Code "View1Controller.cs"
public ActionResult Index ()
{
return View ();
}
Questions.
1. Am I giving everything right?
2. How to make <button> </button>
and<input type = "submit" />
work?Saturday, April 4, 2020 5:10 PM -
User475983607 posted
frmasp897654
2. How to make <button> </button>
and<input type = "submit" />
work?Submit button's along with user inputs must be within an HTML form. The form has an action attribute which POSTs to an Action. This concept is covered in every beginning level tutorial including the link in my first post. The following tutorial cover data access which has examples of HTML form POSTs.
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-3.1
Also, I think you might be interested in learning HTTP basics, GET and POST, in general. GET and POST are fundamental concept in every web application.
Saturday, April 4, 2020 7:31 PM -
User1134142769 posted
frmasp897654
2. How to make <button> </button>
and<input type = "submit" />
work?Submit button's along with user inputs must be within an HTML form. The form has an action attribute which POSTs to an Action. This concept is covered in every beginning level tutorial including the link in my first post. The following tutorial cover data access which has examples of HTML form POSTs.
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-3.1
Also, I think you might be interested in learning HTTP basics, GET and POST, in general. GET and POST are fundamental concept in every web application.
I simplified and did this:
The file code is ".. \ Home \ Index.cshtml"<form method = "post" asp-controller = "View1" asp-action = "Index"> <button> View1_button_3 </button> </form> <form method = "post" asp-controller = "View1" asp-action = "Index"> <input type = "submit" name = "name" value = "View1_submit_3" /> </form>
Code "View1Controller.cs"[HttpPost] public ActionResult Index () { return View (); }
Question.
Did I understand you correctly?Saturday, April 4, 2020 9:24 PM -
User-474980206 posted
you don't link to views. you link to a MVC action, or a razor page. if you are not specifying the route attributes for the action the default route is:
<controller name>/<action name>
there is url helper in view for this:
<a href="@Url.Action("index","home")">Home</a> <a href="@Url.Action("view1","home")">View 1</a> <a href="@Url.Action("view2","home")">View 2</a>
in actions you do a redirect to action. you should follow the post/redirect/get pattern. see:
https://en.wikipedia.org/wiki/Post/Redirect/Get
Saturday, April 4, 2020 11:32 PM -
User711641945 posted
Hi frmasp897654,
1. What HTML page elements to use to follow links to other View?All of the html elements could be used to link to the other view.
Example:
View
<a asp-controller="Home" asp-action="View0">View0</a> <a asp-controller="View1" asp-action="Index">View1/Index</a>
<form asp-controller="Home" asp-action="View0"> <button>View0</button> </form> <form asp-controller="View1" asp-action="Index"> <button>View1/Index</button> </form>
<form asp-controller="Home" asp-action="View0"> <input type="submit" value="View0" /> </form> <form asp-controller="View1" asp-action="Index"> <input type="submit" value="View1/Index" /> </form>Controller
public class HomeController : Controller { public IActionResult Index() { return View();//display the Home/Index.cshtml } public IActionResult View0() { return View();//display the Home/View0.cshtml } }
public class View1Controller : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();//display View1/Index.cshtml
}
}2. What principles are used to switch to View?The fastest way is to use href="url".
3. Ways to click on the links?It depends on your scenario.
4. Is it possible to place several `View` in the folder of one controller?Did you mean you want to put several views in the Views/Home folder and you could render the view by the actions in the HomeController?If so,of course you can.
public class HomeController : Controller { public ActionResult Index() { return View();//display the Home/Index.cshtml } public ActionResult View1() { return View();//display the Home/View1.cshtml } }
Reference:
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-3.1#view-discovery
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 6, 2020 8:11 AM -
User1134142769 posted
2. What principles are used to switch to View?
The fastest way is to use href="url".
I try using "href =" url "" as follows.
View0
<a href="@Url.Action("View0","Home")"> View0 </a> <button onclick = "location.href = '@ Url.Action (" View0 "," Home ")'; return false;"> View0 </button> <input onclick = "location.href = '@ Url.Action (" View0 "," Home ")'; return false;" type = "submit" value = "View0" />
View1
<a href="@Url.Action("View1","Home")"> View1 </a> <button onclick = "location.href = '@ Url.Action (" View1 "," Home ")'; return false;"> View1 </button> <input onclick = "location.href = '@ Url.Action (" View1 "," Home ")'; return false;" type = "submit" value = "View1" />
Questions.
1. Am I using href = "url" "correctly?
2. Are there other common ways to use "href =" url ""?Thursday, April 9, 2020 5:54 AM -
User711641945 posted
Hi frmasp897654,
1. Am I using href = "url" "correctly?Yes,you are.Be sure there is no space in your @Url.Action.
Change:
<button onclick = "location.href = '@ Url.Action (" View0 "," Home ")'; return false;"> View0 </button> <input onclick = "location.href = '@ Url.Action (" View0 "," Home ")'; return false;" type = "submit" value = "View0" />
To:
<button onclick="location.href='@Url.Action("View0","Home")'; return false;"> View0 </button> <input onclick="location.href = '@Url.Action("View0","Home")'; return false;" type="submit" value="View1" />
2. Are there other common ways to use "href =" url ""?You could specify by hard-encoded:
<button onclick="location.href='/Home/View0'"> View0 </button>
Best Regards,
Rena
Thursday, April 9, 2020 6:27 AM -
User1134142769 posted
Rena Ni
Yes,you are.Be sure there is no space in your @Url.ActionUpdate- 1 to the question.
I added a view called "View1_0.cshtml" to the folder "View1" (see the picture of the main question).
Path: \ Views \ View1 \ View1_0.cshtmlDescription.
1. If you have the opportunity, could you rephrase your answer.
I do not understand it all.
"Change:" and "To:" have the same code.
I do not understand. what did you mean by answering the question "1"
2. How to use "href" to call "view" at the prescribed address.
2.1. Example:
Start page:
/Views/Home/Index.cshtml
I am on the start page - "Index.cshtml".
I use the path "/ Home / View1" in the code:<a href="/Home/View1"> View1 </a> <button onclick = "location.href = '/ Home / View1'; return false;"> View1 </button> <input onclick = "location.href = '/ Home / View1'; return false;" type = "submit" value = "View1" />
Result: the code redirects me to the address:
"/Views/View1/Index.cshtml"2.2. Example:
I use the path "/Home/View1/View1_0.cshtml" in the code:<a href="/Home/View1/View1_0.cshtml"> View1_0 </a> <button onclick = "location.href = '/Home/View1/View1_0.cshtml'; return false;"> View1_0 </button> <input onclick = "location.href = '/Home/View1/View1_0.cshtml'; return false;" type = "submit" value = "View1_0" />
Result: the code redirects me to the address:
"/Views/View1/Index.cshtml".Question.
How can I go to the page "/Home/View1/View1_0.cshtml" from the page "/Views/Home/Index.cshtml" using "href"?
Thursday, April 9, 2020 2:39 PM -
User475983607 posted
2.2. Example:
I use the path "/Home/View1/View1_0.cshtml" in the code:<a href="/Home/View1/View1_0.cshtml"> View1_0 </a> <button onclick = "location.href = '/Home/View1/View1_0.cshtml'; return false;"> View1_0 </button> <input onclick = "location.href = '/Home/View1/View1_0.cshtml'; return false;" type = "submit" value = "View1_0" />
Result: the code redirects me to the address:
"/Views/View1/Index.cshtml".Question.
How can I go to the page "/Home/View1/View1_0.cshtml" from the page "/Views/Home/Index.cshtml" using "href"?
Asking this question over and over is not going to change the answer. Views are NOT accessed directly!!! Views are returned by MVC actions. This is a fundamental and openly published concept in MVC. You'll need to accept this fact and move on. Perhaps try reading the reference documentation rather than making up your own constructs.
Thursday, April 9, 2020 2:54 PM -
User-474980206 posted
we keep telling you, you can not link to a view, only an action. if you add view to the view folder, you must create an action that returns that view (the names need not match). your code can then link to that action.
to link to \Views\View1\View1_0.cshtml
if you use standard conventions, because you created view folder named view1, you need a controller named View1Controller and an action named View1_0. then you can link to it with:
@Url.Action("View1_0","View1");
if you don't want to use MVC actions and views, switch to razor pages. the the razor page goes in the Pages folder, and the path is the route (no extension).
https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-3.1&tabs=visual-studio
note: please go thru at least one tutorial so you are not confused by such basic concepts.
Thursday, April 9, 2020 3:03 PM