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
  • 95,331
  • 80
  • 293
  • 426
Fergal
  • 2,484
  • 2
  • 34
  • 47

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
Cloud SME
  • 10,111
  • 3
  • 51
  • 90