I have just gotten started with ASP.NET MVC 5 (C#) and have been a little confused by the HTML helpers.

@using (Html.BeginForm("Add", "Song"))

I understand how this can be helpful, but feel that for people who do not know Razor/C#, that this will be a steep learning curve if not properly exposed. I am also aware of the Tag Helpers that are available and like what I see so far.

<form asp-action="Add" asp-controller="Song">

To me, this seems more appropriate, as it means that people who do not understand Razor syntax will be able to more easily read and understand what is happening, as it bears much more similarity to "normal" HTML. That being said, changing each page to use Tag Helpers seems monotonous and unnecessary.

I have recently found the CodeTemplate files, and included them in my project with the intention of adding the Tag Helpers in the template files. However, I soon realized that most Tag Helpers need a specific Controller or whatever to fill out the element attributes. Obviously, this Controller/whatever will change dynamically depending on which Controller/whatever prompted the creation of the View. If I hardcode asp-controller="Song" and then I try to create an Album View from this CodeTemplate, I obviously will have used the wrong controller.

Is there a way to access the dynamic property of whatever Controller, Link, etc called the View? In other words, is there someway of doing this in the CodeTemplate, where This is the Controller/whatever that constructs the View from the modified CodeTemplate?

<form asp-action="This.Action" asp-controller="This.Controller">

Example:

If I had <form asp-action="This.Action" asp-controller="This.Controller"> in my CodeTemplate in all Views, and then created the Views from different Controllers:

Song Controller

  • Create View - <form asp-action="Add" asp-controller="Song">
  • Update View - <form asp-action="Edit" asp-controller="Song">

Album Controller

  • Create View - <form asp-action="Add" asp-controller="Album">
  • Update View - <form asp-action="Edit" asp-controller="Album">

Is this possible at all? Or am I completely off? Let me know! Thanks!

Related posts

Recent Viewed