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.

JulianK
  • 33
  • 7

2 Answers2

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>
}
Fabjan
  • 13,506
  • 4
  • 25
  • 52
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";
}
Kahbazi
  • 14,331
  • 3
  • 45
  • 76