en / de
Expertisen
Methoden
Dienstleistungen
Referenzen
Jobs & Karriere
Firma
Technologie-Trends TechCast WebCast TechBlog News Events Academy

What are SubmitModels and how to use them

While going deeper in the approach of ViewModels (related to this post) it is also possible to take a look onto SubmitModels. But what are they and how do I use it?

The MVC-Pattern gives the model among other things the task to be a container for data. But it also says that logic, for example the validation-logic, can be done within the model. This is one of the reasons why to use SubmitModels.

The validation has to be done on server side. Period. Having client side validation is nice but like it sounds: It is on the client side. The server should be able to validate everything which comes with a form-submit. So the server-side-validation must be done. Client side is definitely nice…but also a nice to have.

In this following approach every data used in a form and send back to the server after a client submitted the form is stored in a separate model. It is provided on a ViewModel and contains only the data which are modified in the form by the client.

public class LoginViewModel
{
   public LoginSubmitModel SubmitModel { get; set; }
   //... other Properties to build up your View
}
public abstract class SubmitModelBase
{
    public abstract bool IsValid(out IReadOnlyCollection errorMessages);
}
public class LoginSubmitModel : SubmitModelBase
{
   public string UserName { get; set; }
   public string Password { get; set; }

   public override bool IsValid(out IReadOnlyCollection errorMessages)
   {
      List errors = new List();
      if (String.IsNullOrWhiteSpace(UserName))
      {
         errors.Add("Check Username");
      }
      if (String.IsNullOrWhiteSpace(Password))
      {
         errors.Add("Check Password");
      }
      errorMessages = errors;
      return !errorMessages.Any();
   }
}

In this case the SubmitModelBase-Class keeps a collection of ErrorMessages and has an abstract method “IsValid” which has to be implemented by every SubmitModel which inherits.

While building up your view with the ViewModel-data you build up your form with the SubmitModel-data.


The postback can look like this:

[HttpPost]
public ActionResult Login(LoginSubmitModel SubmitModel)
{
   LoginViewModel loginViewModel = new LoginViewModel();
   loginViewModel.SubmitModel = SubmitModel;
   ReadOnlyCollection errorMessages;
   if (!submitModel.IsValid(out errorMessages))
   {
      //Handle Not Valid
   }
   // Handle Valid
}

Conclusion:

With the SubmitModels you can easily encapsulate your form-properties. You can easily validate them (which is a solution if your properties to persist differ from the ones to show. Also the auto-validation from MS keeps a lot of surprises 😉 ) and it gets a better overview of what is going to the client and what comes back.

The inspiration and further information for this Blogentry can be found here (german):
http://benjamin-abt.de/blog/asp-net-mvc-arbeiten-mit-view-und-submitmodels/

Kommentare

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Newsletter - aktuelle Angebote, exklusive Tipps und spannende Neuigkeiten

 Jetzt anmelden
NACH OBEN
Zur Webcast Übersicht