ASP.NET MVC Using Render Partial from Within an Html Helper - asp.net-mvc

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.

Related

ASP.NET Razor C# Html.ActionLink to create empty link

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")

ASP.NET MVC - Converting a complex control to be MVC-friendly

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.

Call one Razor helper from another

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.

MVC 3, Best way to call this HTML

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());
}

How do you localize/internationalize an MVC Controller when using a SQL based localization provider?

Hopefully this isn't too silly of a question. In MVC there appears to be plenty of localization support in the views. Once I get to the controller, however, it becomes murky.
Using meta:resourcekey="blah" is out, same with <%$ Resources:PageTitle.Text%>.
ASP.NET MVC - Localization Helpers -- suggested extensions for the Html helper classes like Resource(this Controller controller, string expression, params object[] args). Similarly, Localize your MVC with ease suggested a slightly different extension like Localize(this System.Web.UI.UserControl control, string resourceKey, params object[] args)
None of these approaches works while in a controller. I put together the below function and I'm using the controllers full class name as my VirtualPath. But I'm new to MVC and assume there's a better way.
public static string Localize (System.Type theType, string resourceKey, params object[] args) {
string resource = (HttpContext.GetLocalResourceObject(theType.FullName, resourceKey) ?? string.Empty).ToString();
return mergeTokens(resource, args);
}
Thoughts? Comments?
I had the same question. This blogpost showed different ways to solve the problem: http://carrarini.blogspot.com/2010/08/localize-aspnet-mvc-2-dataannotations.html
In the end I used a T4 template to generate a resources class. I also have a HtmlHelper method to access my resources:
public static string TextFor(this HtmlHelper html, string resourceName, string globalResourceName, params object [] args)
{
object text = HttpContext.GetGlobalResourceObject(globalResourceName, resourceName);
return text != null ? string.Format(text.ToString(), args) : resourceName;
}
Another version generates a localized version from Controller and View:
public static string LocalTextFor(this HtmlHelper html, string resourceName, params object [] args)
{
string localResourceName = string.Format("{0}.{1}", html.ViewContext.RouteData.Values["controller"],
html.ViewContext.RouteData.Values["action"]);
object text = HttpContext.GetGlobalResourceObject(localResourceName, resourceName);
return text != null ? string.Format(text.ToString(), args) : langName;
}
I use strongly typed resources using PublicResXFileCodeGenerator as Custom Tool (selected in properties of .resx file). If I have resource with 'AreYouSure' name and 'Are you sure?' value, it can be accessed by calling ResourceClass.AreYouSure, both in controller and views. It works pretty well.
What do you want to localize in your controller? Normally only what is shown to the user should be localized. And what is shown to the user should be inside a view so back to the helpers.

Resources