Home TextBoxFor: INumber type but with default value = ""
Reply: 1

TextBoxFor: INumber type but with default value = ""

Ricardo Rocha
Ricardo Rocha Published in 2017-11-10 22:12:53Z

I'm using Asp.net Core 2.0 and I have the following code:

@Html.TextBoxFor(model => model.personalData.sonsNumber, null, new { type = "number", value = "", required = "required", pattern="[0-9]" })

What I'm trying to is to use the validate feature that HMTL5 provides. The problem is that my model.personalData.sonsNumber is an int and has a number as default value and I need that the default value be set as empty string ("").

I already tried the value="" and it is not working.

Can any one tell me how to accomplish that?

Thank you in advance.

Shyju Reply to 2017-11-10 22:28:44Z

You can change the type of your property from int to nullable int (int?)

public class PersonalData
   public int? SonsNumber { set;get;}  // I took the freedom to follow CamelCasing :)

The default value of nullable int will be now NULL so the tag helper/html helper will render empty string for that. Default value of int type is 0

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.29538 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO