0

I need some suggestions to transfer javascript code in html files to typescript files in Visual Studio 2015.

Right now, I have 4 HTML views, where I want to take out the javascript code and convert it to the typescript files. The views are:

  • Travel.cshtml,
  • Hotel.cshtml,
  • Tpa.cshtml,
  • Passengers.cshtml

Every view contains html code + razor and a javascript section.

I'm trying to create 4 typescript files and link them to the view. As the image shows, I have created the files in a folder called "pages". For now they are all just empty :)

enter image description here

How can I link those pages to the representative views, so that the Travel.ts file is linked to the Travel.cshtml file?

Some may ask, why am I doing this? .. I'm doing this to get clean html files with no javascript in, so its easier to read. Also I need to learn the typescript syntax which is a little difficult for me.

Hope someone can help.

1 Answer 1

1

Valid javascript code is valid typescript code. So to get started, you can use your existing javascript code inside your typescript files.

Every time you save your typescript file in visual studio, typescript compiler will be compiling this file and generate the corresponding javascript file with the same name as of your typescript file name, provided you do not have any errors in your typescript file.The generated file will be in the same location of where you have your typescript files are. If you want to see those, you can enable the Show All files option in solution explorer.

enter image description here

You will not include the typescript files in your pages, instead we will use the generated js files. So in your case, in your Hotels view(Hotels.cshtml), you can include it like

@section Scripts
{  
    <script src="~/Content/Scripts/Pages/Hotels.js"></script>
}

Or if you want to bundle it, Add a new bundle in your RegisterBundles() method.

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/hotels").Include(
    "~/Content/Scripts/Pages/Hotels.js"));
   // Other bundles goes here
}

And in your view, use the bundle path

@section Scripts
{  
     @Scripts.Render("~/bundles/hotels")
}
Sign up to request clarification or add additional context in comments.

1 Comment

I finally managed to get use of this and it works. Now I just have to convert my code to typescript :) Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.