1

I have created some C# Methods and I want to use it as reference for my ASP.NET project.

The method give me a string as output. A example for a string: "C:/tracks/audio1.mp3"

Now I try to use my Method "GetTrackPath()" for my html audio tag. Simple -> I want the method output in my

How I can realize it?

ADD CODE

<audio id="a1" preload="auto" controls>
   <source src="GetTrackPath()">
<audio>

Now I want to put the string into the html src tag.

5
  • 1
    I don't understand very well what is the problem, can you provide some code and improve the explanation? Commented May 31, 2016 at 10:57
  • You need to look at creating your own HTML Helper Commented May 31, 2016 at 11:17
  • @JackMorton I hope its more logical now. Commented May 31, 2016 at 11:19
  • Is it Asp.Net MVC or Webforms ? Commented May 31, 2016 at 11:21
  • @Fabjan It is ASP.NET MVC Commented May 31, 2016 at 11:24

2 Answers 2

2

You can create a viewmodel and use it in View :

namespace MyProject.MyNameSpace

public class AudioTrackVM
{
   public string Id { get; set; }
   public string Preload { get; set; }
   public string Src { get; set; }
}

In controller you can add this :

public ActionResult Index()
{
   var model = new List<AudioTrackVM>()  // this is stub, get data from data source instead
   {
      new AudioTrackVM() { Id = "a1", Preload = "auto", Src = @"C:/tracks/audio1.mp3" },
      new AudioTrackVM() { Id = "a2", Preload = "auto", Src = @"C:/tracks/audio2.mp3" }
   };
   return View(model);
}

Now it is time to use this Viewmodel :

@model List<MyProject.MyNameSpace.AudioTrackVM>

@foreach(var audio in Model)
{
   <audio id="@audio.Id" preload="@audio.Preload" controls>
      <source src="@audio.Src">
   <audio>
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can use code tag

write this in you aspx

<audio id="a1" preload="auto" controls>
   <source src="GetTrackPath()">
<audio>

write this method in your aspx.cs

public string GetTrackPath()
{
    return @"C:/tracks/audio1.mp3";
}

Comments

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.