Verification and Exception Handling in MVC and manual activation Verification

Source: Internet
Author: User

Whether you get a model through the action parameter, or manually calling updatemodel () or tryupdatemodel (), the default defaultmodelbinder will automatically perform legal verification. However, when you use other methods to update the model, automatic verification will be skipped. For example:

Public actionresult makebooking (string clientname, datetime? Appointmentdate,

Bool acceptsterms)

{

VaR APPT = new appointment {

Clientname = clientname,

Appointmentdate = appointmentdate. getvalueordefault ()

};

If (! Acceptsterms)

Modelstate. addmodelerror ("acceptsterms", "you must accept the terms ");

If (modelstate. isvalid ){

// To do: actually save the appointment to the database or whatever

// Bypass Verification

Return view ("completed", APPT );

}

Else

Return view (); // re-renders the same view so the user can fix the errors

}

The controller base class provides two basic methods for us to manually trigger verification.

• Validatemodel () verification. If it is invalid, an error will be registered and an invalidoperationexception will be thrown.

.

• The difference between tryvalidatemodel () and validatemodel is that it does not throw an exception, but returns the same value as modestate. isvalide.

Whether you get a model through the action parameter, or manually calling updatemodel () or tryupdatemodel (), the default defaultmodelbinder will automatically perform legal verification. However, when you use other methods to update the model, automatic verification will be skipped. For example:

Public actionresult makebooking (string clientname, datetime? Appointmentdate,

Bool acceptsterms)

{

VaR APPT = new appointment {

Clientname = clientname,

Appointmentdate = appointmentdate. getvalueordefault ()

};

If (! Acceptsterms)

Modelstate. addmodelerror ("acceptsterms", "you must accept the terms ");

If (modelstate. isvalid ){

// To do: actually save the appointment to the database or whatever

// Bypass Verification

Return view ("completed", APPT );

}

Else

Return view (); // re-renders the same view so the user can fix the errors

}

The controller base class provides two basic methods for us to manually trigger verification.

• Validatemodel () verification. If it is invalid, an error will be registered and an invalidoperationexception will be thrown.

.

• The difference between tryvalidatemodel () and validatemodel is that it does not throw an exception, but returns the same value as modestate. isvalide.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.