Questions tagged [razor-declarative-helpers]

Razor View Engine provides the declarative helper syntax which can be used to create helper components that are stored as .cshtml files, and enable re-usability of view code.

46 questions
44
votes
6 answers

Why is the HtmlHelper instance null in a Razor declarative @helper method?

Using MVC 3 RTM I'm getting a strange NullReferenceException: @helper TestHelperMethod() { var extra = "class=\"foo\"";
} It turns out that Html (of type HtmlHelper) is null. I've never seen this before in a…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
32
votes
4 answers

Can I return a string using the @helper syntax in Razor?

I have a RazorHelpers.cshtml file in app_code which looks like: @using Molecular.AdidasCoach.Library.GlobalConstants @helper Translate(string key) { @GlobalConfigs.GetTranslatedValue(key) } However, I have a case where I want to use the result…
30
votes
6 answers

Using @Html inside shared @helper in App_Code

I am building a very basic MVC3 site while I learn and I am having difficulty with the following declarative Razor html helper. Inside RMB.cshtml inside App_Code folder: @helper ReplaceCrLf(string strText) { …
Treborbob
  • 1,221
  • 1
  • 8
  • 18
15
votes
1 answer

ASP.NET MVC - calling Razor @helper from another @helper

I've been implementing some @helper functions in Razor based on Scott Gu's post, and things are going pretty well. What I was wondering though, is if it's possible to call one @helper from another. For example, I have the following helper that…
ataddeini
  • 4,931
  • 26
  • 34
10
votes
1 answer

How to fix error The type or namespace name 'Linq' does not exist in the namespace 'System'

ASP.NET MVC4 application contains Helper.chtml file in App_Code folder containing Razor helpers. After upgrading to Visual Studio Communitity edition compile error Error CS0234 The type or namespace name 'Linq' does not exist in the namespace…
Andrus
  • 26,339
  • 60
  • 204
  • 378
9
votes
1 answer

Using and Creating helper in Asp.net MVC4 Web Application

I am using the Asp.net mvc4 WebRole, and I found this is a Web Application , Not a Web Site. after reading this article, I knew how to add customer Helper function in Asp.MVC4. In Web Site Appliction I can add a cshtml file in app_code folder like…
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
7
votes
3 answers

ASP.NET MVC @helper syntax vs Html Helper Extension methods

I need to create custom html helper method. As far as I know there are two ways: Use @helper razor syntax. http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx Create HtmlHelper extension…
5
votes
0 answers

Can I have private functions or private declarative helpers within a declarative helper?

Possible Duplicate: Using @helpers from another View in Razor ASP.Net MVC3 My environment: ASP.NET MVC3 with Razor. I have some reusable view logic in declarative helpers in the App_Data directory, which are defined as @helper bar() { /* markup…
4
votes
2 answers

How to pass HTML fragment (as a delegate?) to a declarative Razor Helper?

I've been writing some declarative Razor Helpers (using the @helper syntax) for use with the Umbraco 4.7 which now supports the Razor view engine (though I would imagine this applies equally to WebMatrix or ASP.NET MVC). They all work fine. However,…
Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
4
votes
2 answers

What is the "HTML attributes" parameter?

The following sample: @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) appears to be using the second overload of the LabelFor method where the second parameter, htmlAttributes, is documented as An object that contains the…
Old Geezer
  • 14,854
  • 31
  • 111
  • 198
4
votes
2 answers

Add colons and stars to helper generated labels

I'm using Razor and Data Annotations in a .NET 4.5 MVC app. This is from a view model: [Required(ErrorMessage = "Title is required.")] [Display(Name = "Title: *")] public string Title { get; set; } [Display(Name = "Comments:")] …
user1563526
  • 275
  • 2
  • 8
3
votes
1 answer

Can you use a @Helper inside an @Helper?

I am not sure this is possible. I have a bunch of @Helper's inside a view AND in other views: @helper ViewHelper1() { ... } @helper ViewHelper2() { ... } etc. I have repetitive code that is used in the view AND in other views: @if…
REMESQ
  • 1,190
  • 2
  • 26
  • 60
2
votes
3 answers

HTML helpers in ASP.NET MVC 3 with Javascsript action

I have many HTML helper in Helpers.cshtml file, but some of the helper (html) need some jquery action, so how do i can call jquery inside helpers.cshtml, is that possible? i know we can keep the js file in header or particular page, but i do not…
2
votes
1 answer

Using @helpers from another View in Razor ASP.Net MVC3

I want to write a few simple @helpers to use in several views. I want them to be inside a Razor .cshtml file (not in a c# class) to have the HTML syntax highlighted. I can easily access @helpers written within the same View, I can separate them into…
Shaddix
  • 5,901
  • 8
  • 45
  • 86
2
votes
1 answer

How do you generate a hyperlink from a Model in MVC Razor

I want to store website addresses in a table in the database as either "http://some_website.com" or just as "some_website.com". Then in the view I have access to the text through "Model.Website". How can I generate a hyperlink? Is there a helper to…
user646306
  • 503
  • 1
  • 5
  • 16
1
2 3 4