0

I have a funny problem and I did not find the cause. In asp.net MVC application I have a form that saves some simple information. All fields can be stored neatly except one. This field (string) returns validation error "The Int32 field must be a number" but only if the sentences in the text contains a single digit number. For example, if the sentence is as follows: "Some simple sentence that contains the number 3" I'll get a validation error - "The Int32 field must be a number", if that same sentence transformed into:

"Some simple sentence that contains the number 30" or "Some simple sentence that contains a 30%" - no errors

Field property from a model:

    [Display(Name = "Some name")]
    [StringLength(500, ErrorMessage = "You can enter up to 500 characters.")]
    public string Akcija { get; set; } 

Next to that field I have one (string) field with the same property characteristics, and it is working properly.

Clip from view:

<div>

            @Html.TextAreaFor(m => m.Akcija, new { style = "width:500px; height:100px;" })
            @Html.ValidationMessageFor(m => m.Akcija)

        </div>

It can not be simpler than that, but the problem is around here.

Do you have any suggestions?

Edit:

If I keep trying to save the changes it will be saved to the database regardless of the validation error. It seems to me that this is a JavaScript validation error or a bug

Edit 2 - Generated HTML:

            <textarea cols="20" data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." id="Akcija" name="Akcija" rows="2" style="width:500px; height:100px;">

Web aplikacije i siteovi 3 sa 30% sniženja

I do not know where it comes from - this attribute "data-val-number =" The field Int32 must be a number. '" And " data-val-required = "The Int32 field is required.'" I'm not in the code imposed these rules

boris
  • 120
  • 1
  • 2
  • 10

3 Answers3

0

Try tagging the property with [DataType(DataType.MultilineText)]. Maybe that will make it explicit to the TextAreaFor helper.

asymptoticFault
  • 4,491
  • 2
  • 19
  • 24
  • I guess I'll have to use Html.EditorFor with [DataType (DataType.MultilineText)] and write a Template for "Akcija" Why should it be easy when it can be complicated :) . Thank you all for your help and for your time – boris Aug 06 '13 at 18:58
  • Hmm, how about just using a `TextBoxFor` helper in conjunction with the attribute `[DataType(DataType.MultilineText)]` like the solution here http://stackoverflow.com/questions/4927003/asp-net-mvc3-textarea-with-html-editorfor – asymptoticFault Aug 06 '13 at 18:59
  • If you look closely you'll see that it is HtmlEditorFor. TextBoxFor can not be Multiline – boris Aug 06 '13 at 19:02
  • ...And EditorFor can not be applied css unless trough templates – boris Aug 06 '13 at 19:04
0

If anyone ever run into this problem, the workaround is to use Html.EditorFor how it follows:

First: Add validation attribute [DataType (DataType.MultilineText)] to model property so that model property looks like this:

    [Display(Name = "Some name")]
    [StringLength(500, ErrorMessage = "You can enter up to 500 characters.")]
    [DataType(DataType.MultilineText)]
    public string Akcija { get; set; }

Secondly: Make Template for Html.EditorFor. Template create in a folder Views/Shared/EditorTemplates - if there is no folder EditorTemplates, make it.

In the folder EditorTemplates create a partial view named with the name of the model property. In my case it is: akcija.cshtml

In this view should be forwarded the value of the model property and you must include the HTML that defines the field of view so that the template, in my case, look like this:

@model System.String

@if(!string.IsNullOrEmpty(Model))
{
    <textarea name="Akcija" style="width:500px;height:100px;">@Model.ToString() </textarea>
}
else
{
<textarea name="Akcija" style="width:500px;height:100px;">@Model</textarea>
}

Third In your View change Html.TextAreaFor in Html.EditorFor and put a reference to your template. Html.EditorFor, in my case, now looks like this:

<div>
   @Html.EditorFor(m => m.Akcija, "akcija")
   @Html.ValidationMessageFor(m => m.Akcija)
</div>

And that's it, problem solved.

Note: When naming template and objects within the template and the template it self, note the naming convention. Note that my template is named with small letter and in html template there is no id attribute for html object.

I hope that this will help someone

boris
  • 120
  • 1
  • 2
  • 10
0

Use a nullable int/Int32.

public int? mobilequantity { get; set; }

or

public Int32? mobilequantity {get; set;}

that works on my form

Pablo Carpio
  • 75
  • 1
  • 5