0

I am trying to use twitter bootstrap based slider in my mvc project and the razor syntax i used on the textblock is

  @Html.TextBoxFor(model => model.Left,new { @class = "form-control", data-slider-min="0", data-slider-max="20", data-slider-step="1" })

And at runtime its returning error

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

So how can i set the params data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="20" data-slider-step="1" data-slider-value="" on HtmlTextBoxFor Razor element

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • Possible duplicate of [Hyphenated html attributes with asp.net mvc](http://stackoverflow.com/questions/2897733/hyphenated-html-attributes-with-asp-net-mvc). – Zabavsky Jul 15 '14 at 08:25
  • You cannot declare html attributes similar to html tags inside razor syntax. As mentioned in the post provided by **Zabavsky**, you have to follow razor syntax to set html attributes. Your syntax should be as below: **`@Html.TextBoxFor(model => model.Left,new { @class = "form-control", data_slider_min="0", data_slider_max="20", data_slider_step="1" })`** – D_Learning Jul 15 '14 at 12:55

1 Answers1

3

data-slider-min is being interpreted as data - slider - min (i.e. data minus slider minus min etc)

Razor allows for hyphen/minus ("-") characters by translating underscores to hyphen/minus characters in the output of attribute names...

e.g. use underscores:

@Html.TextBoxFor(model => model.Left,new { @class = "form-control", data_slider_min="0", data-slider-max="20", data-slider-step="1" })

re: the actual error reported: Are you using a strongly typed model? You must for the various "for" extension methods to work (as they generate an expression tree).

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • With a slight changed, this works perfectly: `code` @Html.TextBoxFor(model => model.Slider,new { @class = "slider", data_slider_min="1", data_slider_max="999999", data_slider_step="1", data_slider_id="blue" }) – cornelha Sep 30 '14 at 09:50
  • @cornelha: Please share any changes. This was typed from memory :) – iCollect.it Ltd Sep 30 '14 at 09:51
  • I hit enter too fast lol. The basic change is to set @class="slider", the rest of the data attributes are pretty self explanatory. In my theme, the data-slider-id just denotes the color – cornelha Sep 30 '14 at 09:54
  • Since I am on a roll here, the TwitterBootstrapMVC code: @Html.Bootstrap().TextBoxFor(m => m.Slider).Class("slider").Data(new { slider_id = "blue", slider_max = "999999" }) – cornelha Sep 30 '14 at 09:58
  • @cornelha: Ah, I as just focusing on the problem reported. Thanks :) – iCollect.it Ltd Sep 30 '14 at 10:14