0

I'm using MVC4.

I have the following element in Razor View:

@Html.DropDownListFor(m => m.Planning_Control_Resi1, Model.Planning_Control_Resi1List)

and in Model the following functions:

         public struct RugItem
         {
             public float value { get; set; }
             public string name { get; set; }
         } 
         public float Planning_Control_Resi1 { get; set; }
         public IEnumerable<SelectListItem> Planning_Control_Resi1List
         {
             get
             {
                 List<RugItem> listItems = new List<RugItem>();
                 float valuef = 0.05f;
                 for (int i = 0; i <= 20; i++)
                 {
                     listItems.Add(new RugItem
                     {
                         name = valuef + "",
                         value = valuef
                     });
                     valuef += 0.05f;
                 }
                 return new SelectList(listItems, "value", "name");
             }
         }

The full form is:

@using (Html.BeginForm("RunRug", "Impressions", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.LabelFor(m => m.Planning_Control_Resi1, "Planning_Control_Resi1")
    @Html.DropDownListFor(m => m.Planning_Control_Resi1, Model.Planning_Control_Resi1List)
    <br />
    <input type="hidden" name="cookie_token" id="cookie_token" />
    <input type="submit" value="Run Rug"  />
}

The HTML produced by the previous code is :

<option value="0,05">0,05</option>
<option value="0,1">0,1</option>
<option value="0,15">0,15</option>
<option value="0,2">0,2</option>

My regional settings for decimal symbol is , and I can't change it. The problem is that the value isn't sent back to controller because it can't convert string format ("0,05" to double).

How can I convert the value from options from string to double in a DropDownListFor element?

Thank you,

banuj
  • 3,080
  • 28
  • 34
  • Do you mean your getting a client side validation error that prevent you submitting the form? –  Nov 01 '15 at 23:53
  • No, I don't receive any validation error, but the form action isn't called any more. – banuj Nov 01 '15 at 23:55
  • You have not shown your form :) And have you enabled client side validation? Show more of your view so we can see where the problem is –  Nov 01 '15 at 23:57
  • 1
    You code will work fine if (1) the server culture is one that accepts a comma as the decimal separator, and (2) client side validation is disabled (or you override the validator method for `number). What culture have you specified in the `web.config` file? –  Nov 02 '15 at 00:10
  • Thank you Stephen for your comment. Indeed the problem was that that field was validated by default as number. I've disabled the default client side validation and now it's ok. Thank you again for this answer. – banuj Nov 02 '15 at 00:18
  • It would be better to include client side validation and override the validator or use the `jquery.globalize.js` plugin (refer [this answer](http://stackoverflow.com/questions/7295385/asp-net-mvc3-double-validation-comma-point-null) for an example –  Nov 02 '15 at 00:22
  • 1
    And the code in your controller can simply be `List listItems = new List(); float valuef = 0.05f; for (int i = 0; i <= 20; i++) { listItems.Add(valuef); valuef += 0.05f; } return new SelectList(listItems);` –  Nov 02 '15 at 00:25

0 Answers0