Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable default validation for dropdown

Tags:

asp.net-mvc

I have a view Known as Machinefilter

@using (Html.BeginForm())
{
    <div id="filterDiv">
        <fieldset>
            <legend>Filter</legend>
            <table>
                <tr>
                    <td>
                        @Html.Label("Machine Serial No :")
                    </td>
                    <td>
                        @Html.TextBoxFor(m => m.MachineSrNo, new { @id = "SearchSerialNo" })
                    </td>
                    <td>
                        @Html.Label("City :")
                    </td>
                    <td>
                        @Html.DropDownListFor(m => m.CityId, new SelectList(ViewBag.Cities, "CityId", "CityName"), "--Select City--", new { @id = "drpCity" })
                    </td>
                    <td>
                        @Html.Label("Bank Branch :")
                        </td>
                        <td>
                        @Html.DropDownListFor(m => m.BankBranchId, new SelectList(ViewBag.BankBranch, "BankBranchId", "Name"), "--Select Bank Branch--", new { @id = "drpBankBranch" })
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    <p>
    <input type="submit" value="Search" />
    <input type="reset" value="Clear" />
</p>
}

Here whenever I click on "Search" button its shows default validation. Where i have not put any validation for this view.

like image 846
NewToBirtReporting Avatar asked Oct 19 '25 09:10

NewToBirtReporting


2 Answers

ASP.NET MVC implictly does couple of validations even you don't decorate the properties with validation attributes. The couple of validations are type checking and required. These two validations will happen if you use non-nullable built-in data types like integer, datetime etc.

I hope you have properties as type integers. An integer property should have some value if you don't submit the implicit validation fails and you get an error.

To avoid this you can use nullable types.

For ex.

public class Model
{
   public int? CityId{get;set;}
}
like image 138
VJAI Avatar answered Oct 22 '25 05:10

VJAI


For MVC5, just add data_val = false to the helper you do not want validation on.

@Html.TextBoxFor(m => m.MachineSrNo, new { @id = "SearchSerialNo", data_val = false  })
like image 37
Jason Honingford Avatar answered Oct 22 '25 03:10

Jason Honingford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!