Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 below so that I can use my custom helper method in another cshtml.

  @helper  HotDeployButton(string value, string url , bool enable= true){

      string enablestr = string.Empty;
      if (!enable)
      {
          enablestr = "disabled=\"disabled\"";
      }

      <input type="button"  name="@value" value="@value" onclick=" window.location.href='@url'" class="mobile-button"  @enablestr />
  }

  @helper Img(string picName, string alt ){

      string root = "/content/themes/default/images/";
      string imgurl = root + picName;

      <img  alt="@alt" src="@imgurl" title="@alt" />

  }

In Another cshtml shown below will use HotDeployButton method.

<div class="bottom-div">
            @Html.Hidden("hdSelMinorPackId", "")
            <!--Html.Hidden("randomId", (object)ViewBag.RandomId)-->
            <input type="submit" name="ExcuteDeploy" id="ExcuteDeploy" value="Deploy" onclick="return validateVersion();"
                class="mobile-button" />
            &nbsp;&nbsp;&nbsp;&nbsp;
            @Helpers.HotDeployButton("Back", Url.Action("Index"))
        </div>

But in Asp.net Web Application there is no App_code Folder in project. I don't know how to make it in a Web Application . please help me .Thanks

like image 276
Joe.wang Avatar asked Oct 15 '12 01:10

Joe.wang


People also ask

What is helper in ASP.NET MVC?

HTML Helpers are managed within View to execute HTML content. We can use HTML Helpers to implement a method that returns a string. This tutorial discusses how you can define or create custom HTML Helpers that you can use within your MVC views.

How many helpers are there in MVC?

There are three types of built-in HTML helpers offered by ASP.NET.

Can we create custom HTML helper?

Creating HTML Helpers with Static MethodsThe easiest way to create a new HTML Helper is to create a static method that returns a string. Imagine, for example, that you decide to create a new HTML Helper that renders an HTML <label> tag. You can use the class in Listing 2 to render a <label> .


1 Answers

The directory is not created with the default Web Application project template so you just need to create the App_Code folder by hand and it should work (right click on the project -> Add -> New Folder).

If you've done it right it will have a special icon:

enter image description here

By the way this is the first step in your linked tutorial:

Creating a Helper

This procedure shows you how to create the helper that creates the note, as just described. This is a simple example, but the custom helper can include any markup and ASP.NET code that you need.

  1. In the root folder of the website, create a folder named App_Code. This is a reserved folder name in ASP.NET where you can put code for components like helpers.
like image 130
nemesv Avatar answered Sep 27 '22 22:09

nemesv