.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Razor. Display conditional Html

Posted By:      Posted Date: September 18, 2010    Points: 0   Category :ASP.Net
Hello,I have the following code on a View using Razor view engine: @if(View.Section == CMSSection.Home) { Html.ActionLink("Home", MVC.CMS.Home.Index()) } else { <span>Home</span> }I keep getting the error:Compiler Error Message: CS1002: ; expectedWhat am I missing?Thank you,Miguel

View Complete Post

More Related Resource Links

Using Dynamic Property inside Html Helper with Razor fires an error



I have the following with Razor:

@Html.GoogleAjax(View.GoogleAjaxKey, new String[,] { { "jquery", "1.4.2" } })

And the I get a strange error:

  'System.Web.Mvc.HtmlHelper<object>' has no applicable method named 'GoogleAjax' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

What is strange is that it works fine using:

@Html.GoogleAjax("123", new String[,] { { "jquery", "1.4.2" } })

I tried the following code line:


And the key is displayed just fine so the problem is how I use the value of @View.GoogleAjaxKey into Html.GoogleAjax.

Do I need to change something on my Html Helper? Here it is:

    public static MvcHtmlString GoogleAjax(this HtmlHelper helper, String key, String[,] libraries) {


Conditional styles for selected Html.ActionLink


Hi, I am trying to add an item selected class to an Html.ActionLink, I am very new to MVC so its all a bit confusing.

So far i have each of my ActionLinks lookins like this

<li><%: Html.ActionLink("Home", "Index", "Home", new { @class = ViewData["current"] == "Home" ? "selected" : "" })%></li>

And in the controller i have

public ActionResult Index() {
            ViewData.Add("current", "Home");
            return View();

Can any one tell where i am going wrong because the links now fail to respond, basically it isn't checking the condition == Home and if it is then its failing because it doesn't get to the controller.

Any ideas would be appreciated


Razor. Conditional Rendering.



I am using Razor and I need to display a String or the view property value on the following condition:

@if(String.IsNullOrEmpty(View.Title)) { @("Default Title") } else { @View.Title }

This is working. But can I make this simpler?

I tried the following:

@String.IsNullOrEmpty(View.Title) ? @("Default Title") : @View.Title

This is not working ...



display codes (asp or html) on web page

hello.....i am wishing to display asp or html codes on my web page. actually i am working on article project and article includes codes too. still i am using <iframe> but not looking soo good.i am also wishing to dispaly codes in a way that can be expand or collapsed using (+) sing, as you also know. or tell me any alternative to accomplish. thanks in advance

convert doc to html and display it


i have create a small program that upload a document to a file path and then convert it to an html doc in whith it create me an html file and a folder with the same name that include the style of the document and photos if in the original document they are any photos.

what i want to know is if we upload the document and they are photos when i click on the html file that is been create i can see the photos, but when i include the html file in my asp.net code i can not see the photos and only the text document is been show.

is it possible to view and the photo or not?


MVC3 Razor - Option to Not have Html encoding


Since all calls to methods (using Razor View Engine and syntax) that return strings have their result Html encoded, is there a way (or an option) to not have the return values html encoded?

For example if I have a simple method like so:

protected string RenderTable(IEnumerable<T> data)
  /* Return some html */

and then in a view (using the Razor View engine) I have code like this:


I'd like the returned string to be inserted into the view as-is and not Html encoded.

What are my options?

Conditional Hide/Display of fields


I am trying to create a form. Unfortunately, the form has a lot of fields and my user requested some field to be displayed based on "Yes" or "No" input. For example we have a field to capture input for "Is there extended warranty?" If user chose "yes", a new field get displayed showing a new line which user must enter some text about the extension"


I am using tables to lay out the form. I found that it is necessary to add the hidden field to a new row so that it can be hidden. I tried using Javascript and CSS to hide/show the row but I am looking for an alternative approach as I dont find this very efficient.


Any ideas?


Thanks in advance.  

Display html.actionlink through c


Hello guys,

I am noob and am trying to display Html.Actionlink through controller(MVC). Here is the example what am trying to do...

public string test(){

     string testString = "<%: html.ActionLink('click', 'test', new AjaxOptions() { UpdateTargetId = 'test' }) %>";

     return testString;


i want to display testString in view. but am not getting the actionlink in view..i can display anchors which is given below:-

public string test(){

     string testString = "<a href ="test"> click here</a>";

     return testString;


Hope reader is understanding what am trying to say...

any help would be appreciated.



Html Display


I am passing JSON data to client side usign ajax call

In callback i get the result like

 success: function (txt) {

ln list there is two other list Grade,Title

Like list --> Grade --> 2,3
       Title --> Maths,Science

now i want to make Html for this

like Grade, Course
      2   Maths
 3   Science

How can i achive this using jQuery or JS to make html and display it

Display Binary PDf in HTML/Text



I am saving user uploaded PDF files in SQL server using varbinary datatype. There is totally no problem in saving in binary and retrieving in PDF. But due to one specific requirement (which i cant change) i to need to fetch binary PDF and display in HTML or atleast get in string form.

Is it possible? If yes how.

Rite now i m opening PDF files like code written below. But it opens a PDF reader in browser. I want to display the data of PDF in plain text instead in PDF reader.

bytedata = (byte[])dsPrograms.Tables[0].Rows[0]["PDFFile"];
                PdfReader pdfReader = new PdfReader(bytedata);
                Response.ContentType = "application/pdf";
                PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream, '\0', true);
                AcroFields pdfFormFields = pdfStamper.AcroFields;
                AcroFields docFields = pdfStamper.AcroFields;
                pdfStamper.FormFlattening = false;

Razor needs shorter syntax to prevent HTML encoding


It's great that Razor HTML encodes by default. However, many times I have HTML in a database and want to display it literally on a page. In WebForms 4, we can use <%= %> and <%: %> to choose between encoding options. Raven's syntax is currently @(new HtmlString(Model.Greeting)). This is far too verbose.

Please add a shorter syntax to Razor. Something like @=Model.Greeting or @@Model.Greeting, or something else.


Conditional HTML


I am designing the website for my local table tennis club. Each year, there is a league. In each league, there can be three divisions, though sometimes there can be 2. There will never be less than 2 and no more than 3. There is a database table for each. There is a gridview for each on the page. If there are no records in the table, the gridview will not show. There is also a <h2> tag above each gridview. When there aren't any records in the gridview for division 3, I want the <h2> tag to disappear. Is there a way to do this? 

Publishing HTML and RichTextField display HTML code - not rendering


Hi there, 

I have several custom columns of type "Publishing HTML", and a page layout several controls which point to the columns. :

<SharePointWebControls:RichTextField DisableInputFieldLabel="True"  FieldName="WheelImage" runat="server"></SharePointWebControls:RichTextField>


The problem is that in the end result, the page shows escaped HTML (eg. <p></p>) rather than the rendered output I would expect. 


Oddly - when it edit mode - they render fine, showing the rendered output. But in the 'reader' view, it shows the raw HTML. 


I have also checked the 'edit html source' in edit mode - and it looks fine, like HTML. But the browser source of the output page shows escaped html: &lt;br/&gt;


I found this describing the same problem, but its not a solution - as this happens on any browser:




Anyone any ideas?






RichHtmlField showing html markup in display mode



Can somebody provide a solution to my problem? I have a RichHTMLField that I created in sharepoint designer/Visual Studios. After I got everything to work. I've tested it on several computers and it worked fine. When I tried it on my computer, my RichHtmlField in my sharepoint  shows html markup in display mode. In the edit mode, it shows perfectly fine and well formatted. When I save and publish it out, the published mode shows the actual HTML markup like you would see in WYSIWYG interface.

Any solutions?

ASP.NET, HTML, JavaScript Snippet Support (VS 2010 and .NET 4.0 Series)

This post covers another useful improvement in VS 2010 - HTML/ASP.NET/JavaScript snippet support. Snippets allow you to be more productive within source view by allowing you to create chunks of code and markup that you can quickly apply and use in your application with a minimum of character typing.

Visual Studio has supported the concept of "snippets" for VB and C# in previous releases - but not for HTML, ASP.NET markup and JavaScript. With VS 2010 we now support snippets for these content types as well.

GridView inline Master/Detail record display

This code drop is part of a smash and grab series. If you're in a rush, you can just grab this code and insert it into your application, no understanding is required. When you have some time (ya, right) you may want to review the source code.

Maintain HTML Radio Button Selection Inside GridView After Postback

Long time ago I wrote an article about getting the value of the selected RadioButtons inside the GridView control. The code did not addresssed the postback issue which means that the selected radio button was cleared after the postback occurs.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend