razor-declarative-helpers

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

廉价感情. 提交于 2019-12-23 06:47:13
问题 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 as the link text in an @Html.ActionLink(...) . I cannot cast the result to a string. Is there any way to return plain strings from Razor helpers so that I can use them both in HTML and within an @Html helper? 回答1: Razor helpers return HelperResult

Can you use a @Helper inside an @Helper?

强颜欢笑 提交于 2019-12-10 17:48:46
问题 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 (!(Model.Entity == Model.Enum.One)) { <td> @ViewHelper1() </td> } else { <td> @ViewHelper1() </td> <td> @ViewHelper1() </td> } The actual @ViewHelper1 has more complex code, but that's not important (I think). Well, since each view has a number of @Helper 's (30+

HTML helpers in ASP.NET MVC 3 with Javascsript action

旧城冷巷雨未停 提交于 2019-12-08 03:06:17
问题 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 want to do like that, i want to use jquery or javascript only on the page which loaded particular helper. anyone have idea on this? My scenario is, i have list box control, that is properly loading from helper, but i need to apply custom theme to the

HTML helpers in ASP.NET MVC 3 with Javascsript action

帅比萌擦擦* 提交于 2019-12-06 11:51:52
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 want to do like that, i want to use jquery or javascript only on the page which loaded particular helper. anyone have idea on this? My scenario is, i have list box control, that is properly loading from helper, but i need to apply custom theme to the list box. Little more Clarity //in index.cshtml @Helpers.testListBox("mylist" "1,2,3,4,5,6,7") //in

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

给你一囗甜甜゛ 提交于 2019-12-01 10:46:05
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 Helpers.cshtml , and if I put this Helpers.cshtml into an App_Code folder I can access it from any View via @Helpers.MyHelper() . But I want them to be accessible only for a few pages. I think, it could be like putting a @using if the helpers are in c# class, but what is the namespace for just another view?.. Sorry, but that's not possible with

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

爷,独闯天下 提交于 2019-12-01 07:44:10
问题 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 Helpers.cshtml , and if I put this Helpers.cshtml into an App_Code folder I can access it from any View via @Helpers.MyHelper() . But I want them to be accessible only for a few pages. I think, it could be like putting a @using if the helpers are

Using @Html inside shared @helper in App_Code

南楼画角 提交于 2019-11-30 11:22:32
问题 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) { @Html.Raw(Html.Encode(strText).Replace(Environment.NewLine, "<br />")); } Inside my index.cshtml view: @RMB.ReplaceCrLf(Model.Post) This gives me a null reference exception on Html in the helper, because it doesn't seem to know what it is. I can work around this by passing Html from the

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

a 夏天 提交于 2019-11-29 00:58:02
Using MVC 3 RTM I'm getting a strange NullReferenceException : @helper TestHelperMethod() { var extra = "class=\"foo\""; <div @Html.Raw(extra)></div> } It turns out that Html (of type HtmlHelper ) is null . I've never seen this before in a regular view. I'm starting to experiment with declarative helper methods in Razor (so far they seem a little limited) and I'm quite stumped by what I'm seeing here. Darin Dimitrov That's a known limitation of those helpers . One possibility is to pass it as parameter: @helper TestHelperMethod(HtmlHelper html) { var extra = "class=\"foo\""; <div@html.Raw

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

可紊 提交于 2019-11-27 15:30:01
问题 Using MVC 3 RTM I'm getting a strange NullReferenceException : @helper TestHelperMethod() { var extra = "class=\"foo\""; <div @Html.Raw(extra)></div> } It turns out that Html (of type HtmlHelper ) is null . I've never seen this before in a regular view. I'm starting to experiment with declarative helper methods in Razor (so far they seem a little limited) and I'm quite stumped by what I'm seeing here. 回答1: That's a known limitation of those helpers. One possibility is to pass it as parameter: