Html.ActionLink() vs. <a> tag - asp.net-mvc

In ASP.NET MVC, for linking standard pages (the ones that does not need special parameters or query string), is there any situation where I should prefer Html.ActionLink() to a standard Link tag?
Thanks.

#Html.ActionLink is tied into the MVC routing definitions. It isn't just a helper for writing an anchor tag, it uses routing to determine what the href looks like and how it's structured.
By using ActionLink you insure all your links are rendered based on how your MVC routes are configured.
Routing is powerful and can prevent the need of having to have lots of query string variables or hidden fields to pass around data.
Use #Html.ActionLink, in the end the extra effort is negligible.

Its a matter of preference.
Html.ActionLink() is just a HTML Helper which will ultimately render a <a> tag.
I prefer using <a> tag.

Related

Nesting elements using razor markup

Am working on an Asp.Net MVC Web application, I am using a theme to replace the default theme and I would like to use #html.ActionLink() to generate a link. My problem is that the theme requires that I nest another element in the html link tag that will be automatically be generated. How do I nest elements using razor syntax?
I don't think that you can nest elements in razor syntax.
Solution for your problem would be to use #Url.Action() inside href property of a tag like this:

html.beginform v old skool form tag

What is the benefit to using Html.BeginForm?
We're having some issues related to html helpers and razor generator view testing and I'm struggling to see the benefit which would stop us going back to old skool form tags.
Has anyone got an argument for or against either?
by old skool i mean:
<form action="#Url.Action('Blah')">
The Html.BeginForm is useful because it generates the url using the routes defined in the Global.asax. (or you can extend it with your own code)
Using the old tag is neither worst or best in my opinion. You simply have to generate your url manually or using the Url helper. In the end the html in the page will be the same
<form ....>
html
</form>
Html.BeginForm also implements IDisposable, meaning the form must be closed properly. It's a minor thing, perhaps, but not closing Html.BeginForm produces a run-time error, where an unclosed <form> tag does not.
no there is no difference , the form tag just use the routing to generate the url , so if you use #Url.Action you are good to go
there is even books use that way a plain old tag and a url helper to generate the route
ASP.NET MVC Website Programming is an example
Edit
**
starting from Mvc 4 there is no difference , prior to Mvc 4 , Mvc 3 for example require the Html.BeginForm to make the javascript unobtrusive validation to work

How do I substitute dynamic parameter values to T4MVC using razor?

Consider following:
$("#myform").attr({ action: "#Url.Action(MVC.Thing.Delete().AddRouteValue("id", myJsModel.Id )) });
I'm trying to set the action method of the form to a strongly typed T4MVC route. How do I insert a dynamic value from javascript into the route value?
I've seen the use of #: but I don't know how to insert it back into razor.
I don't think that using T4MVC here versus regular MVC syntax makes much difference when it comes to this issue.
When thing you might try is to generate a replaceable token on the server and do the replacement client side. e.g. something like
MVC.Thing.Delete().AddRouteValue("id", "SOMETOKEN")
And then take the generated path on the client and replace "SOMETOKEN" with myJsModel.Id.

Building pagination in MVC: Is there anyway to get something like an Html.HyperLinkFor

What I mean by that is a type element that will have a label based on a value in my viewModel and also be able to submit that value back up to the viewModel so it can grab new results based on Current Page and Page Size. This is for creating a gridview in MVC that supports pagination.
None of the examples I've seen of MVC so far have had anything resembling a gridview. It's important that I create my own paging and not use any built in paging mechanisms or third party controls or html helpers or the like
I'd go with ActionLink. To build Urls you can either use Url.Action or Html.BuildUrlFromExpression(c => c.conTrollerAction)
If you use T4MVC, you will get some nice helpers that do exactly what you are looking for.
Something along the lines of:
<a href="<%: Url.Action(MVC.MyController.MyAction(Model.ActionMethodParam1, Model.ActionMethodParam2)) %>">
You can also use the Html.ActionLink helper:
<%: Html.ActionLink("Link Name", MVC.MyController.MyAction(Model.ActionMethodParam1, Model.ActionMethodParam2)) %>
T4MVC is a great little library that I use in every MVC app.

ASP.NET MVC: How to allow some HTML mark-up in Html Encoded content?

Is there some magic existing code in MVC 2 to Html.Encode() strings and allow certain html markup, like paragraph marks and breaks? (coming from a Linq to SQL database field)
A horrible code example to achieve the effect:
Html.Encode(Model.fieldName).Replace("<br />", "<br />")
What would be really nice is to overload something and pass to it an array (or object) full of allowed html tags.
It's not a good idea to create your own whitelist based on regular expressions because you'll likely inadvertently open a security hole for XSS.
From Sanderson's book "Pro ASP.NET MVC3 Framework": "...The only viable mitigation is strict, whitelist-based filtering: use a library like the HTML Agility Pack to ensure the user-supplied markup contains only the tags that you explicitly allow."
Sanderson goes on to supply a link to a site that demonstrates a broad range of XSS techniques that you'd have to test for if you use the regex approach. Check out http://ha.ckers.org/xss.html
There is nothing built in to ASP.NET or MVC for this, but it's not that hard to write your own whitelist-based one with regular expressions and so on. Here's one that Jeff wrote, though it's pretty rough around the edges...
I can't think of anything off the bat but I guess you could write an extension method that allows you to add a paremeter/list of items to allow.
Html.Encode(Mode.fieldName, List<items> Myitems);
It could modify the allowable tags into < etc and then encodes the rest like normal.

Resources