2

I have an error validation issue with an int.

I have validation for a customer name:

if (String.IsNullOrEmpty(CustomerName))
            yield return new RuleViolation("Customer Name Required", "CustomerName");

now if I want to add validation for a city, in this case I have a CityID that gets saved as type int, so I can't write my if statement the same way...because there's no int.IsNullOrEmpty method. Let's say the user didn't select the dropdown for city - it's basically saving with no value.

What's the best way to write my validation statement for an int?

UPDATE:Here's a sample of what I have for my form in my view:

<% using (Html.BeginForm())
   {%>

    <fieldset>
        <legend>Add a new customer</legend>
        <p>
            <label for="CustomerName">Customer Name:</label>
            <%= Html.TextBox("CustomerName")%>
            <%= Html.ValidationMessage("CustomerName", "*")%>
        &nbsp;&nbsp;
            <label for="CityID">City:</label>
            <%= Html.DropDownList("CityID", Model.Cities as SelectList, "Select City")%>
            <%= Html.ValidationMessage("CityID", "*")%>
        &nbsp;&nbsp;
            <input type="submit" value="Create New Customer" />
        </p>
    </fieldset>

<% } %>

and my view model looks like this:

public class CustomerFormViewModel
{
    //Properties
    public Customer Customer { get; set; }
    public SelectList Cities { get; set; }

    //Constructor
    public CustomerFormViewModel(Customer customer)
    {
        CustomerRepository customerRepository = new CustomerRepository();
        Customer = customer;
        Cities = new SelectList(customerRepository.FindAllCities(), "CityID", "CityName");  
    }
}

4 Answers 4

1

use nullable ints,

int? i = null;
Sign up to request clarification or add additional context in comments.

Comments

1

I had this same exact requirement a week ago. You can solve this in two ways.

1) In Dropdown you will always have "Please Select" with default value as -1. When a form is submitted and no city is selected then MVC model will bind -1 (default value) for CityID. So you can always check if CityID > 0 else "raise error"

2) Use c# 3.0 feature Nullable Int (int?) for CityID which means your CityID can also be null. If no value is passed for CityID, your cityID will always be null.

2 Comments

I appreciate your help, although I'm not quite understanding how to give "Please Select" it's default value of -1. I am able to get it to say "Please Select", but I don't know where to specify what it's value is. I updated my question with the code that's in my view to show what I currently have.
When you create a dropdown, it will have Text and Value. I always assign Value as -1 and Text as "Please Select". When a form is submitted, the posted value will contain "-1" as the CityID in your example.
0

You can set a default value in the route config. If you set the default to a value you know is invalid then you can just check for that value. If your cities are numbered 1 through X then set the default at -1 and check for that.

Comments

0

If you get the input in a string format, then try to use regular expression to validate the integer format.

Regex rxNums = new Regex(@"^\d+$"); // Any positive decimal
if (!rxNums.IsMatch(cityId))
{
    yield return new RuleViolation("city id Required", "cityId");
}

If you get the value in the integer format, then you just check if the value is larger than 0 or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.