razor

MVC-3 and Html.Serialize (ASP.NET 4, MVC3)

孤人 提交于 2019-12-23 08:37:12
问题 Currently working on a project in MVC-3. Trying to put the following code in my view, but Visual Studio is telling me it can't find the Serialize method. @Html.Serialize("User",Model) (ex) @using (Html.BeginForm()) { @Html.Serialize("User",Model) <fieldset> ... </fieldset> } Is this feature missing in MVC-3? I see examples of people using it in MVC-2. Perhaps there is a new way of handling this issue? 回答1: Html.Serialize is in the Futures assembly. 回答2: Install the Nuget package: Install

ASP.NET MVC3 Razor - Maintain scroll position on postback

徘徊边缘 提交于 2019-12-23 08:11:41
问题 How can i maintain scroll position on postback after sorting a grid table that uses MvcContrib framework? 回答1: The usual way is to use some javascript to set the current scroll position to a hidden field, then restore that position on page load (usually in a jquery ready event). However, that's really just a side effect. You should be doing some kind of ajax command to update the grid rather than a postback, then no scrolling required. 回答2: Use jQuery and client side cookie. $(function(){ var

Formatting strings in ASP.NET Razor

醉酒当歌 提交于 2019-12-23 07:56:19
问题 I am currently writing a small templating system in ASP.NET to allow users to add content. For example, the user can enter the string (variable type is string). topHeader[x] = "They think it's all over. It is now!"; However, one change that's needed is the ability to add some basic HTML tags within this content, so the following can be done topHeader[x] = "They think it's all over. <strong>It is now!</strong>"; or topHeader[x] = "They think it's all over. <a title="Football News" href="URL"

Remove blank/empty entry at top of EnumDropDownListFor box

老子叫甜甜 提交于 2019-12-23 07:55:27
问题 I am rendering a drop down list box using my enums and I only have 3 options, but for some reason it is displaying four. The top and default option is simply blank/empty and I want this removed. I want the top/default value to be 'Option1'. Enums: public enum EventType { [Display(Name = "Option 1")] Option1, [Display(Name = "Option 2")] Option2, [Display(Name = "Option 3")] Option3 } View: @Html.EnumDropDownListFor(model => model.EventType, null, new { @id = "eventType", @class = "form

How to include an MVC Razor variable within Url.Content?

℡╲_俬逩灬. 提交于 2019-12-23 07:49:37
问题 Using MVC 3 Razor, how can I specify a variable within a call to @Url.Content() . Example: @{ var myVar = Request.QueryString["foo"]; } <a href="@Url.Content("~/bar?@myvar")">click here</a> 回答1: You're already in the c# context at that point, so you can use the variable just like you would in a code file. Try this: <a href="@Url.Content("~/bar?" + myvar)">click here</a> 来源: https://stackoverflow.com/questions/6066002/how-to-include-an-mvc-razor-variable-within-url-content

ASP.NET MVC: Cannot use a lambda expression as an argument to a dynamically dispatched operation

走远了吗. 提交于 2019-12-23 07:29:12
问题 I have a ASP.NET MVC4 Application. My view get a List from my controller. I want to select these list with lambda expression but I get the following error: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type List<project.Models.LAYER> layers = new List<project.Models.LAYER>(); layers = @Model.layers.Select(x => x.KONT == "EUROPE"); @Model.layers is a List NOW I TRIED THAT: BUT THE SAME ERROR: @{

Print and/or modify the c# version that the razor compiler service uses to compile cshtml

十年热恋 提交于 2019-12-23 07:00:58
问题 I'd like to be able to find out which C# version razor uses to compile my cshtml templates. The reason why I want this, is this breaking change. We had a lambda in a foreach statement that worked fine on our local dev machines but produced a bug on our test environment (which doesn't have C# 5 installed). This bug was VERY hard to debug (we even copied all the test environment DLLs and databases and were still not able to reproduce the bug). So to prevent this dev/test difference in the

Syntax to concatenate a variable with static html

孤者浪人 提交于 2019-12-23 06:55:14
问题 I'm trying to build up a bit of HTML using a mix of razor variables and static content. Here is where I get stuck: I have a counter variable in my view called section_counter . I want to use that in the ID of the image tag I'm building. However unlike with <% .. %> notation I'm used to, I'm just not able to do what I need. <img alt="section" id="@section_counter_Section" src=""..... etc I need the id to look like 3_Section . However if I leave a space between the variable and the word

Syntax to concatenate a variable with static html

牧云@^-^@ 提交于 2019-12-23 06:54:17
问题 I'm trying to build up a bit of HTML using a mix of razor variables and static content. Here is where I get stuck: I have a counter variable in my view called section_counter . I want to use that in the ID of the image tag I'm building. However unlike with <% .. %> notation I'm used to, I'm just not able to do what I need. <img alt="section" id="@section_counter_Section" src=""..... etc I need the id to look like 3_Section . However if I leave a space between the variable and the word

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