3

I'm building a form in Razor like below:

@using (Html.BeginRouteForm("foo", new { controller = "foo", action = "bar" }, FormMethod.Post, new { id="foo", enctype="multipart/form-data", accept-charset="utf-8" }))
{       
    <label for="file">File</label>
    <input type="file" name="file" id="file" />
    <input type="submit" value="Send"/>
}

I need to get some attributes in the form tag. But the compiler doesn't like the dash in accept-charset. How might I allow an object property in C# have a dash?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Fergal
  • 2,484
  • 2
  • 36
  • 48

1 Answers1

4

Use an underscore in the property name: accept_charset

MVC automatically convert underscores in html attribute properties to dashes:

@using (Html.BeginRouteForm("foo", new { controller = "foo", action = "bar" }, FormMethod.Post, new { id="foo", enctype="multipart/form-data", accept_charset="utf-8" }))
{       
    <label for="file">File</label>
    <input type="file" name="file" id="file" />
    <input type="submit" value="Send"/>
}

Credit: How to use dashes in HTML-5 data-* attributes in ASP.NET MVC

Community
  • 1
  • 1
Andy T
  • 10,223
  • 5
  • 53
  • 95