Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC Error Validation
    primarykey
    data
    text
    <p>I have an error validation issue with an int. </p> <p>I have validation for a customer name:</p> <pre><code>if (String.IsNullOrEmpty(CustomerName)) yield return new RuleViolation("Customer Name Required", "CustomerName"); </code></pre> <p>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.</p> <p>What's the best way to write my validation statement for an int?</p> <p>UPDATE:Here's a sample of what I have for my form in my view:</p> <pre><code>&lt;% using (Html.BeginForm()) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Add a new customer&lt;/legend&gt; &lt;p&gt; &lt;label for="CustomerName"&gt;Customer Name:&lt;/label&gt; &lt;%= Html.TextBox("CustomerName")%&gt; &lt;%= Html.ValidationMessage("CustomerName", "*")%&gt; &amp;nbsp;&amp;nbsp; &lt;label for="CityID"&gt;City:&lt;/label&gt; &lt;%= Html.DropDownList("CityID", Model.Cities as SelectList, "Select City")%&gt; &lt;%= Html.ValidationMessage("CityID", "*")%&gt; &amp;nbsp;&amp;nbsp; &lt;input type="submit" value="Create New Customer" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; </code></pre> <p>and my view model looks like this:</p> <pre><code>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"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload