ASP.NET MVC HtmlHelper.ActionLink replace %20 with + - asp.net-mvc

If I have a url generated like this
<%=Html.ActionLink("Link name", "MyAction", "MyController", new { SomeParameter = "value with spaces" })%>
is it possible to easily generate the output html like so
<a href="/MyController/MyAction/value+with+spaces">
instead of
<a href="/MyController/MyAction/value%20with%20spaces">
Or am I best looking at overloading the ActionLink method and replacing those characters when returning the string?

Or am I best looking at overloading
the ActionLink method and replacing
those characters when returning the
string?
Yes.
The easier way is to just make a space-dash replacer extension method. Or just call Replace manually.
<%=Html.ActionLink("Link name", "MyAction", "MyController", new { SomeParameter = "value with spaces".Replace(" ", "-" })%>

Related

asp.net mvc optional params in razor actionLink

I have 2 links for language switch
<a class="dropdown-item"
href="#Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "en" }, null)"
style="color:#333;">English</a>
<a class="dropdown-item"
href="#Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "ar" }, null)"
style="color:#333;">Arabic</a>
it works fine there is only controller and action in url
but when there is optional param like id for detail and edit action than it do not work as expected.
I think I have to change null (this last param) with something but I am new and googled a lot but not getting anything worthy, Please help me.
It would be better if the solution work for n number of optional params instead of only one Id, but for now that will also be acceptable.
it would be better if the solution work for n number of optional params instead of only one Id
for getting all passed querystring parameter on MVC controller side better to use
Request.QueryString
Request.QueryString is NameValueCollection and you get value passed in querystring parameter in your actionlink.
i have tried that looks like below
<a class="dropdown-item" href="#Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "ar",id="100",studentid = 1,studentName = "abc" }, null)" style="color:#333;">Arabic</a>
and your mvc Controller look like below
public ActionResult About(int id)
{
var querystring = Request.QueryString;
// in querystring you get all value like below screenshot
var studentName = querystring["studentName"]; // access parameter like
ViewBag.Message = "Your application description page.";
return View();
}
you can get all parameter that you passed in your controller.

ASP .NET MVC ActionLink

I'm trying to make the (2) red and bold in the below code but it just displays the tag instead. I tried Html.Raw() but it returns IHtmlString that can't be used in ActionLink.
#{String surveyCount = "Survey <b>(2)<b>"; }
#Html.ActionLink(surveyCount, "Index", "Student")</a></li>
#{
string surveyCount = 2;
}
Survey <b>#surveyCount</b>
can be like this
Survey <span class="danger">(2)</span>

master page link generation wrong - s#arp architecture

I am using s#arp architecture 2.0 with asp.net mvc 3.0. The razor code in a master page looks like this:
#Html.ActionLink("Logout", "LogOff", "Users", new { style = "color:Blue;" })
For some reason the app does not produce the correct link anymore (to the action logoff of controller users) but rather points to the current controller for the action logoff. I have not changed anything. where do I have to dig to overcome this please?
The generated link looks like this:
CurrentControllerName/LogOff?Length=5
You are calling a wrong overload of the ActionLink helper. Here's what you do:
#Html.ActionLink(
"Logout", // linkText
"LogOff", // actionName
"Users", // routeValues
new { style = "color:Blue;" } // htmlAttributes
)
It's pretty obvious why this doesn't produce the correct url. You are passing "Users" which is a string value at the place where the helper expects routeValues which must represent an anonymous object.
The correct overload is:
#Html.ActionLink(
"Logout", // linkText
"LogOff", // actionName
"Users", // controllerName
null, // routeValues
new { style = "color:Blue;" } // htmlAttributes
)

MVC3 Weird behavior of ActionLink with HTML 5 Data- attributes

When I use this helper method to create a link, the data attribute shows up correctly in HTML code:
#Html.ActionLink("Test", "Index", null, new { data_something = "123" })
The HTML is correct:
<a data-something="123" href="/">Test</a>
When I use the following overload of the ActionLink method (I use the T4MVC script, http://mvccontrib.codeplex.com/wikipage?title=T4MVC), the data attribute contains an underscore instead of a dash:
#Html.ActionLink("Test", MVC.Home.Index(), new { data_something = "123" })
The HTML is incorrect:
<a data_something="123" href="/">Test</a>
Is this a know bug or a feature? I searched the bugtracker (http://aspnet.codeplex.com/workitem/list/basic) but was not able to find a corresponding issue.
The following overload is working again, but I don't like to create Dictonaries all the time:
#Html.ActionLink("Test", MVC.Home.Index(), new Dictionary<string, object> {
{ "data-something", "123" }
})
for data attribute use #data_something="123" like
#Html.ActionLink("Test link",
MVC.Home.Index(),
new {controller="Home"}},new {#data_something="123"})
the above code should output
Test Link>

How do you add a RouteValueDictionary to a T4MVC ActionLink

There seems to be no extension method to include some arbitrary route values that normally I would expect to go into the querystring.
Old code:
<%: Html.ActionLink(Model.ParentMessage.Category.Text, "index", null, new { category = Model.ParentMessage.CategoryID }, new { })%>
I want to change it to this but it takes the category as an HTML attribute.
<%: Html.ActionLink(Model.ParentMessage.Category.Text, MVC.Feedback.Index(), new { category = Model.ParentMessage.CategoryID })%>
Just checking this isn't already possible before I write my own extension method as surely this was already accounted for?
Yes, it's there! :) See the doc (http://mvccontrib.codeplex.com/wikipage?title=T4MVC_doc). Look for "Adding additional route parameters".
e.g. either:
<%: Html.ActionLink(Model.ParentMessage.Category.Text, MVC.Feedback.Index().AddRouteValue(category, Model.ParentMessage.CategoryID))%>
or
<%: Html.ActionLink(Model.ParentMessage.Category.Text, MVC.Feedback.Index().AddRouteValues(new { category = Model.ParentMessage.CategoryID }))%>

Resources