Reputation: 731
I'm new to trying out MVC and Razor. Currently using Visual Studio for Mac preview to develop a website to try out the technology.
My biggest concern is how do you change pages? When I'm comparing this to MVVM, where you only markup your front-end you'd be declaring a button in XAML-code and then just calling it in a method in your code behind.
I have two Views Index.cshtml and Company.cshtml I want to go to Company when you press the continue button. This is my code..
<input
type="submit"
class="button button-block"
value="Continue"
onclick="window.location.href='<%= Url.Action("Company", "HomeController") %>';" />
And then from what've understood as best practice is to define the method in the controller? So this is where I dont really understand how to connect these 2.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
// do some stuff here to go the Company page
}
}
How are they connected? Can you only write all your Front-end in HTML and CSS and then just all your back-end with C#? Or do you have to mix them?
Thanks!
Upvotes: 1
Views: 758
Reputation: 11480
Your approach should vary with the site you intend to build. For instance, the following:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
public class CompanyController : Controller
{
public ActionResult Index()
{
return View();
}
}
The above would generate your routes, http://example.com/Home
and http://example.com/company
but when your button within your view fires:
<a href="/Company/Index">Company</a>
This will trigger a flicker, because the user is awaiting the server to re-render the page. Including the shared data that will be bundled and minified from your home page. So an alternative, would be a single page application.
You have your api controller, then you utilize JavaScript to:
$.ajax({
// Ajax that will return a .json object to build the page.
});
Which would call a template engine, or maybe you use a frontend framework like Angular, Ember, or React. But by letting the backend be independent, the frontend will handle all the transitions for you. But this will keep your application without the postback. It will act as a single page application.
Another approach.
Upvotes: 0
Reputation: 236268
You should have Company
action (i.e. method) in HomeController
:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Company()
{
return View();
}
}
This action will be called when you click the link. Note that you also need corresponding view. And one more note - you should not use Controller
suffix in controller name when generating action url. Here is correct Razor syntax:
onclick="window.location.href='@Url.Action("Company", "Home" )';"
Upvotes: 1