ASP .NET MVC ActionLink - asp.net-mvc

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>

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.

How to pass Controller Element value to View(.cshtml) div element value?

I developing WEB API project using .net C#
I need to get the value from controller.cs file and use that value into View(.cshtml) file.
My Controller Code:
HomeController.cs
XmlDocument doc = new XmlDocument();
doc.Load(httpWebResponse.GetResponseStream());
XmlElement TransID = (XmlElement)doc.SelectSingleNode("/CensusUploadResponse/TransactionID");
if (TransID != null)
{
string ResultID = TransID.InnerText;
}
I need to pass the ResultID to Index.cshtml(Ex:ResultID = 17)
MY View .cshtml file code:
Index.cshtml
<div runat="server" id="CensusuploadDiv" style="border:1px solid black; width:420px;">
</div>
I need to assign that ResultID value to CensusuploadDiv.
I try with following method using ViewBag. but's it's not working.
Assign ResultID to ViewBag like below
if (TransID != null)
{
string ResultID = TransID.InnerText;
ViewBag.Test2 = ResultID;
}
and i get the value in index file like below
<div runat="server" id="CensusuploadDiv" style="border:1px solid black; width:420px;">
#ViewBag.Test2
</div>
But value not bind to div.
I find out solution for my problem by Using TempData.
TempData help me lot...
I use TempData as follow in HomeController.cs
string result = strBind.ToString();
TempData["Result"] = result;
In view.cshtml
#TempData["Result"]
It's work fine for me...
Do you develop Web API or ASP.NET MVC project? Best way to access values from controller in view is to use strongly typed models:
#model int
<div>#Model</div>
Example:
Index.cshtml
#model int
<div>#Model</div>
HomeController.cs
public ActionResult GetId()
{
int id;
....
return View(id);
}
Please note, that WebApi and MVC controllers are slightly different. But this is how it works.

Actionlink with text & icon?

I have this link in clean HTML. I want to make it into a MVC ActionLink. But how is that possible. I cant understund, seems there is no easy way. OR?
<li class="active"><i class="icon-home icon-large"></i> Dashboard</li>
Any ideas?
You have several options for doing this.
Option 1:
<li class="active"><i class="icon-home icon-large"></i> Dashboard</li>
Option 2:
Write your own extension method for HtmlHelper, that takes in a String/MvcHtmlString and put it into the link tag, without html encoding it.
public static MvcHtmlString SpecialActionLink(this HtmlHelper html, String action, String controller, String innerHtml) {
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", new UrlHelper(html.ViewContext.RequestContext).Action(action, controller));
tag.InnerHtml = innerHtml;
return new MvcHtmlString(tag.ToString(TagRenderMode.Normal));
}
(Haven't tested it, but should give you an idea on how to do it.)
<li class="active">
#Html.ActionLink("Dashboard", //Title
"Index", // Action
"Home", // Controller
new { #class = "home-icon"} // htmlAttributes
)
</li>
.home-icon{ /*Some CSS to set the background image (not tested) */
padding:16px;
background:transparent url('path-to-image.png') .... ;
}
You Can't use ActionLink. only way is #url.action
<span>This is a test</span>

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>

ASP.NET MVC HtmlHelper.ActionLink replace %20 with +

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(" ", "-" })%>

Resources