I am trying to create a Razor web helper something like this :
#helper DisplayForm() {
#Html.EditorForModel();
}
But this gives the error "CS0103: The name 'Html' does not exist in the current context".
Is there any way to reference html helpers within web helpers?
You can cast the static Page property from the context to the correct type:
#helper MyHelper() {
var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;
Html.RenderPartial("WhatEver");
#Html.EditorForModel();
}
Declarative helpers in Razor are static methods. You could pass the Html helper as argument:
#helper DisplayForm(HtmlHelper html) {
#html.EditorForModel();
}
#DisplayForm(Html)
Razor inline WebHelper is generate static method.
So can not access instance member.
#helper DisplayForm(HtmlHelper html){
#html.DisplayForModel()
}
How about this?
Related
I have created a razor helper using the
#helper MyHelper(string param) {
}
syntax. I need to be able to access the model state to determine if I should add error classes to the elements. How would I access this? Intellisense does show ModelState but it is always null.
In a razor page I would use ViewData.ModelState but ViewData doesn't exist in the context.
You need to explicitly pass the view context from the view when you call this helper method.
#helper MyHelper(string param,ViewContext context) {
<div>
#foreach (var modelStateVal in context.ViewData.ModelState.Values)
{
foreach (var error in modelStateVal.Errors)
{
<p>#error.ErrorMessage</p>
}
}
</div>
}
and in the view where you want to call this,
#MyHelperClass.MyHelper("Hello",this.ViewContext)
Another option is to create an Html Hepler method
In our ASP.NET MVC3 project we have written couple of custom HTML Helper extension method, which basically renders some composit controls (say a text and a label with some needed styles). Now we also want to render some javascript along with HTML tags, but looks MVCHtmlString does not render javascript test as javascript ! Any options or alternatives to render dynamic javascript from custom HTML Helpers ?
It works fine for me :)
here is what I used as an extension method:
namespace MvcApplication1.ExtensionMethods
{
public static class MyExtensionMethods
{
public static MvcHtmlString SomeJavascript(this HtmlHelper helper)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script> alert('testing 123')</script>");
return MvcHtmlString.Create(sb.ToString());
}
}
}
and in my index.cshtml i call it like this:
#using MvcApplication1.ExtensionMethods
....
#Html.SomeJavascript()
and it shows me the pop-up :)
I have a strongly-typed Razor 2 editor template. When the view model meets certain conditions, I want the view to delegate to an alternative template of the same type. I use the TemplateName argument to the EditorFor helper to select the alternative template:
#model MyType
#if (Model.IsSpecialCase)
{
#Html.EditorFor(m => m, "SpecialCaseTemplate")
}
else
{
#* Default markup *#
}
Problem is, Razor does not call the alternative template; it simply passes over the EditorFor method. If I change the type of the second template, it shows it correctly. I can work around this using a Partial View, but I would rather not, as I have a scheme going with Editor Templates that I want to stick to.
Anyone know how I can get this to work?
Edit
Looks like this has something to do with the behaviour described here: ASP.net MVC - Using EditorFor with the same model type twice. In short, that MVC does not support using the EditorFor method on the same object twice.
The best way to do this, is by returning a different view from your Controller:
public ActionResult someaction(){
var Model = ...;
if (Model.IsSpecialCase){
return View("SpecialCaseTemplate");
}
else{
return View();
}
}
Alternatively, you could do in the view like this:
#model MyType
#if (Model.IsSpecialCase)
{
Html.RenderPartial("SpecialCaseTemplate", model);
}
else
{
#* Default markup *#
}
Say I want to write an HTML helper to draw a chart.
This helper would need to emit specific JS script code into the body of the page it was called from.
Is this recommended ?
If not how would i workaround that ?
Can i tell the helper to emit the code into a specific section defined in the main layout ?
This should do it:
public static class HtmlHelperExtensions
{
public static MvcHtmlString HelloWorld(this HtmlHelper helper)
{
return new MvcHtmlString("alert('Hello World');");
}
}
In Your Razor View:
<script type="text/javascript">
#Html.HelloWorld();
</script>
You probably should not be doing this.
I would consider JS front end content that forms part of your view. Your view should simply display your view model.
I would be interested in knowing why you feel you need to be able to deliver JS in this way?
Creating MVC3 Razor Helper like Helper.BeginForm()
says that it can be done using extension methods and implementing IDisposable. Can the same be done by using declarative Razor helpers eg. #helper SomeHelper(){}?
Sort of.
Razor helpers return raw HTML, not IDisposable, so you can't use it with using.
However, you can create a Razor helper in App_Code to render the content, then call it from a normal extension method that writes the content directly to the page and returns a separate IDisposable.
No because helper.BeginForm() requires the IDisposable method to write out the closing </form> tag. As a razor helper is essentially a method and not a class it can't implement IDisposable.