How would you use an Html.ActionLink to render the following link -
It may seem silly to do this, but sometimes I need a link that has link functionality (rollover pointers, etc.) but does not go anywhere. And I want to use an Html.ActionLink for code consistency.
I have tried different variations of Html.ActionLink but I keep getting messages about things not allowed to be null.
#Html.ActionLink(" ", "", "", new {href="javascript:void(0)"})
will render as
Instead of forcing ActionLink to do something it isn't made for, consider creating your own helper method:
public static class MyHtmlExtensions
{
public static MvcHtmlString EmptyLink(this HtmlHelper helper, string linkText)
{
var tag = new TagBuilder("a");
tag.MergeAttribute("href", "javascript:void(0);");
tag.SetInnerText(linkText);
return MvcHtmlString.Create(tag.ToString());
}
}
Import the namespace into your view and you'll be able to do this:
#Html.EmptyLink("My link text")
Related
I have an ASP.NET WebForms control (derived from Control, not WebControl, if it helps) that has a rather complicated Render() function. The control has no viewstate and only uses the Control approach so it can render output directly. I feel it's a fine candidate for working with the MVC approach.
I'd like to use it in an MVC application I'm using, however I don't know what's the best way to go about it.
At first I thought about converting it to a HTML Helper method, but my control renders a large amount of HTML so the Helper method (with it returning strings) isn't too attractive.
The alternative is a PartialView, but those are UserControl derivatives, which wouldn't be appropriate in this case.
I see other HTML Helper methods don't return HtmlString, but actually use HtmlHelper.ViewContext.Writer to write output directly, but according to this question ( HtmlHelper using ViewContext.Writer not rendering correctly ) he was getting strange results. I'd like to avoid that mishap.
EDIT:
I think I've solved it using the HtmlHelper.ViewContext.Writer approach, and I haven't experienced the same problem as the problem I quoted.
Here's the code I wrote:
public static class MiniViewHelper {
public static void RenderMiniView<TModel>(this HtmlHelper html, MiniView<TModel> view, TModel model) {
TextWriter wtr = html.ViewContext.Writer;
HtmlTextWriter hwtr = wtr as HtmlTextWriter;
if( hwtr == null ) hwtr = new HtmlTextWriter( wtr );
view.Render( hwtr );
}
}
public abstract class MiniView<TModel> {
public TModel Model { get; set; }
public abstract void Render(HtmlTextWriter wtr);
}
public class VeryComplicatedMiniView : MiniView<ComplicatedViewModel> {
public override void Render(HtmlTextWriter wtr) {
wtr.WriteLine("VeryComplicatedMiniView ");
}
}
Used like so from my pages:
<% Html.RenderMiniView( new VeryComplicatedMiniView () { Propery1 = foo }, Model.RelevantMiniViewModel ); %>
Any thoughts?
The two approaches you have outlined in your question are correct. You could either try to write a custom HTML helper which will spit the same HTML as the control or use a partial.
I see other HTML Helper methods don't return HtmlString, but actually
use HtmlHelper.ViewContext.Writer to write output directly
ViewContext.Writer should be fine. Returning an IHtmlString from the helper is also fine. Just make sure you are properly encoding it inside since IHtmlString will not be automatically HTML encoded in Razor and it supposes that the helper takes care of this. Using a TagBuilder to generate a DOM tree in a helper is a good approach.
I need to create the second overload of Razor helper and want to call one helper from another (with some specific parameters). Is there any way to implement it?
Sure:
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static class ActionLinkExtensions
{
public static IHtmlString MyActionLink(this HtmlHelper html)
{
// call the base ActionLink helper:
return html.ActionLink("some text", "someAction");
}
}
and then in your view:
#Html.MyActionLink()
If you are talking about #helper Razor helpers you need to pass an instance of the HtmlHelper as argument because it is not available in the helper context:
#helper MyActionLink(HtmlHelper html)
{
#html.ActionLink("some text", "someAction")
}
and then:
#MyActionLink(Html)
Personally I prefer the first approach as it is view engine agnostic and can be ported across any other view engines you like whereas the second is Razor specific and if tomorrow Microsoft invent the Blade view engine you will have to rewrite much of your code.
Creating a site in MVC 3 and I have this code snippet that I use on several parts on the design. It's a designcode that creates a head for modules on the site.
Now wondering if this is the best way to call this code snippet? Should i use Helpers or is there a better way?
Today I do like this:
public static IHtmlString FrameModuleHeader(this HtmlHelper helper, int Type, string Headline)
{
StringBuilder html = new StringBuilder();
html.Append("<div class='module_top'>");
html.Append("<div class='module_top_left'></div>");
html.Append("<div class='module_top_middle_" + Type + "'><div class='module_top_headline'><h4>" + Headline + "</h4></div></div>");
html.Append("<div class='module_top_right'></div>");
html.Append("</div>");
return MvcHtmlString.Create(html.ToString());
}
And then call my HTML helpers in the view through:
#Html.FrameModuleHeader(1,"My headline")
Thanks!
/Mike
I would probably use a partial view or a display template that I would include instead of HTML helper because stuffing so much HTML in a C# code looks ugly.
So for example I would have a view model:
public class SomeViewModel
{
public int Type { get; set; }
public string Headline { get; set; }
}
and a partial:
#model AppName.Models.SomeViewModel
<div class="module_top">
<div class="module_top_left"></div>
<div class="module_top_middle_#(Model.Type)">
<div class="module_top_headline">
<h4>#Model.Headline</h4>
</div>
</div>
<div class="module_top_right"></div>
</div>
and then:
#Html.Partial("Foo", new SomeViewModel { Type = 1, Headline = "My headline" })
This of course doesn't mean that your HTML helper wouldn't work. It's just that normally HTML helpers should be used to generate small fragments of HTML and in this particular example this doesn't seem to be the case. Also you get HTML intellisense if used in a view which might aid you identify unclosed tags, not properly formatted HTML, ... whereas inside C# everything is one big magic string.
And one final remark about your HTML helper if you decide to use it: Make sure you HTML encode this Headline string before putting it inside the string builder or you might get bad XSS surprises.
There is really no recommended way. The way that you are doing it is very clean. So I see no reason to change it.
Another option is to use a Partial View. You can put it in /Views/Shared so it is available to everything. One reason this is nice, is it can be more easily edited without having to push out a new code base. If this output never changes, then perhaps that's not necessary.
You could of course cure the code bloat caused by divitus, by removing the unnecessary html tags.
public static IHtmlString FrameModuleHeader(this HtmlHelper helper, int Type, string Headline)
{
StringBuilder html = new StringBuilder();
html.Append("<div class='module_top'>");
html.Append("<h4 class='module_top_middle_" + Type + "'>" + HtmlEncode(Headline) + "</h4>");
html.Append("</div>");
return MvcHtmlString.Create(html.ToString());
}
O.K. I think that's an easy one!
I have a ViewMasterPage with some links on it (Home/About/Login/ etc). What I want to achieve is simply have the link disabled when it's referral url is already opened (i.e. if the current url is /Register then the Register link should be disabled .. easy hah!?)
As I don't like to write a lot of in-line coding inside of my views, I end up extending HtmlHelper with some extension methods (just to keep the code in .cs files) and in my views I call these methods, here's my register method as an example:
public static string Register (this HtmlHelper html)
{
TagBuilder builder ;
if (HttpContext.Current.Request.Url.AbsoluteUri.ToUpperInvariant().Contains(MainLinks.Register.ToUpperInvariant()))
return MainLinks.Register; // will return the string "Register"
builder = new TagBuilder("a");
builder.InnerHtml = MainLinks.Register;
builder.AddCssClass("register");
builder.Attributes.Add("href", "/register/");
return builder.ToString();
}
Though this works, it still has two problems:
The hard coded string values of the urls (specially for the home link as I compare the AbslouteUri with "http://www.mysite.com/")
My programming instinct doesn't like it, I feel it should be much simpler than that.
Any ideas!
Ps: No javascipt allowed! It's a javascript-free version of the application.
I don't see too much wrong with this, it's clear to see what it does and it works. However, it's probably better to make it a bit more reusable as I can imagine you repeat yourself a bit with the other links. Maybe something like:
public static string RenderLink(this HtmlHelper html, string text, string url, object htmlAttr) {
if (!HttpContext.Current.Request.Url.AbsolutePath.StartsWith(url, StringComparison.InvariantCultureIgnoreCase)) {
return text; //comparison excludes the domain
}
TagBuilder tag = new TagBuilder("a");
tag.SetInnerText(text);
tag.Attributes.Add("href", url);
//... add attributes parsed as htmlAttr here
return tag.ToString();
}
Then add your links to your view like:
<%= Html.RenderLink("Register", "/register/", new { #class="register"}) %>
<%= Html.RenderLink("Account", "/account/", new { #class="account"}) %>
If you wanted to get away from the hard coded domain, then using Request.Url.AbsolutePath instead of AbsoluteUri as above achieves that.
The alternative would be to parse the current page information in the model from the controller, maybe like ViewData.Model.CurrentPage = "Register";, but I wouldn't advise you doing that as I don't see it being the job of the controller in this case.
I have an HtmlHelper extension that currently returns a string using a string builder and a fair amount of complex logic. I now want to add something extra to it that is taken from a render partial call, something like this ...
public static string MyHelper(this HtmlHelper helper)
{
StringBuilder builder = new StringBuilder();
builder.Append("Hi There");
builder.Append(RenderPartial("MyPartialView"));
builder.Append("Bye!");
return builder.ToString();
}
Now of course RenderPartial renders directly to the response so this doesn;t work and I've tried several solutions for rendering partials to strings but the all seem to fall over one I use the HtmlHelper within that partial.
Is this possible?
Because this question, although old and marked answered, showed up in google, I'm going to give a different answer.
In asp.net mvc 2 and 3, there's an Html.Partial(...) method that works like RenderPartial but returns the partial view as a string instead of rendering it directly.
Your example thus becomes:
//using System.Web.Mvc.Html;
public static string MyHelper(this HtmlHelper helper)
{
StringBuilder builder = new StringBuilder();
builder.Append("Hi There");
builder.Append(helper.Partial("MyPartialView"));
builder.Append("Bye!");
return builder.ToString();
}
I found the accepted answer printed out the viewable HTML on the page in ASP.NET MVC5 with for example:
#Html.ShowSomething(Model.MySubModel, "some text")
So I found the way to render it properly was to return an MvcHtmlString:
public static MvcHtmlString ShowSomething(this HtmlHelper helper,
MySubModel subModel, string someText)
{
StringBuilder sb = new StringBuilder(someText);
sb.Append(helper.Partial("_SomeOtherPartialView", subModel);
return new MvcHtmlString(sb.ToString());
}
You shouldn't be calling partials from a helper. Helpers "help" your views, and not much else. Check out the RenderAction method from MVCContrib (if you need it now) or MVC v2 (if you can wait a few more months). You'd be able to pass your model to a standard controller action and get back a partial result.