How do I substitute dynamic parameter values to T4MVC using razor? - asp.net-mvc

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.

Related

How to generate a URL for ASP.NET MVC without string names

How to generate a URL for ASP.NET MVC without string names? I.e. I have Index action in HomeController. Using strings I would go for
#Url.Action("Index","Home")
but I would like to avoid use of string. I remember seeing a new way how to do this without strings, but I cannot find it anywhere now. The only code that I have found was
#(Url.Action<HomeController>(x=>x.Index()))
but that does not work for me. Error given is
The non-generic method 'System.Web.Mvc.UrlHelper.Action()' cannot be used with type arguments.
My ASP.NET MVC version is 5.2.3. If the above is only for 6+, is there any way to make achieve what I want?
You might want to check out T4MVC: https://github.com/T4MVC/T4MVC
You need a nuget package for that:https://www.nuget.org/packages/Microsoft.AspNet.Mvc.Futures/
this way you can do things like this:
<%= Html.ActionLink<MyController>(x => x.MyMethod(a), "text") %>
<%= Html.BuildUrlFromExpression<MyController>(x => x.MyMethod(a)) %>

Html.ActionLink() vs. <a> tag

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.

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

Asp.net mvc3 Hash(#) handing in routing system

I use location.hash javascript mathod a lot at my ajax requests. Generally I replace standard
{action}/{id} with {action}#{id}
How can I introduce this strategy at rounting system?
I wol like write:
#Html.ActionLink(text, action, controller, new { id }, new { })
and this generates /controller/action#id ?
Oh may be it's not good idea?
How do you process ajax requests(open some entity at list?)
Thanks
The answer you seek is here:
How to access AJAX hash values in ASP.NET MVC?
TL;DR - Its impossible.
Remember: Routing isn't just for generating links; it's also for parsing incoming URIs. Incoming URIs don't have hash/fragments on them, so the route you propose, if it were possible, wouldn't work for incoming requests.
You can write your own HTML helper, in lieu of Html.ActionLink to create the URI with the hash, if you want, but it can't be a route.
this should work as you want.
RedirectResult(Url.Action("{action}") + "#{id}");

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