Search code examples
c#htmlrazorasp.net-core-mvc

.NET Core MVC - renders accept Attribute String wrongly


I have an Filechooser and in this Filechooser I set the accepted Property from a ViewData. Which works without problems. On another Site I have an Filechooser too, which uses the same ViewBag, but is not bound to an ViewModel.

var List<string> Allowed =new List<string>() { ".txt",".doc" };
...
ViewData["Allowed"]=String.Join(", ", Allowed.ToArray());
....
 <input asp-for="NewFiles" class="file-select" multiple accept=@ViewData["Allowed"]> //Works
....
 <input name="Files" id="Files" type="file" class="file-select" multiple class="file-select" accept=@ViewData["Allowed"]> //Does not work

In the first Variant the accept Attribute is rendered to: ".txt, .doc". While in the second One it will be: ".txt",.doc". Meaning only "txt" will be available in the Chooser. Why?

I have used the Debugger to check what is in the ViewData and what will be written into the "accept" in the View and in both cases it is the same String.

How can I fix the second?


Solution

  • Use double quotes:

    <input name="Files" id="Files" type="file" multiple class="file-select" accept="@ViewData["Allowed"]"> 
    

    This syntax will render the following <input> tag:

    <input name="Files" id="Files" type="file" multiple class="file-select" accept=".txt, .doc">