Where do you put your check in asp.net mvc 3?

A common recommended practice in asp.net mvc is that you should not submit your business models to your views . Instead, you should create viewmodels specific to each view.

When this is done, and you call the ModelState.IsValid method in your controller, you effectively verify that the viewmodel is correct, but not the business object.

What is the usual approach to solving this issue?

public class Person
{
public int ID {get; set;};

[Required]
public string Name {get; set;}

[Required]
public string LastName {get; set;}

public virtual ICollection<Exam> Exams {get; set;}

}

public class PersonFormViewModel
{

public int ID {get; set;};    


[Required]
public string Name {get; set;}

[Required]
public string LastName {get; set;}

}

This is exactly what I have right now, but I'm not sure that the [Required] attribute should appear on both models or just ViewModel or just Business Model.

Any advice on this issue is welcome.

Additional links to support my claim that viewing models are always recommended.

POCO ()

http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx

+5
5

- .

, , , , .., .

, , -/ , "", ( ), .

"Validate", . , , .

, , .

:

, , "" (, , ..):

public abstract class Post
{
   // .. fields, properties, domain logic, etc

   public void Validate()
   {
      if (!this.GeospatialIdentity.IsValidForThisTypeOfPost())
         throw new DomainException(this, BusinessException.PostNotValidForThisSpatial.);
   }
}

, - . DomainException , . BusinessException, . enum .

, , , " ", , , , , .

:

[HttpPost]
public ActionResult Create(QuestionViewModel viewModel)
{
   if (!ModelState.IsValid)
     return View(viewModel);

   try
   {
      // Map to ViewModel
      var model = Mapper.Map<QuestionViewModel,Question>(viewModel);

      // Save.
      postService.Save(model); // generic Save method, constraint: "where TPost: Post, new()".

      // Commit.
      unitOfWork.Commit();

      // P-R-G
      return RedirectToAction("Index", new { id = model.PostId });
   }
   catch (Exception exc) 
   {
      var typedExc = exc as DomainException;

      if (typedExc != null)
      {
         // Internationalised, user-friendly domain exception, so we can show
         ModelState.AddModelError("Error", typedExc.BusinessError.ToDescription());
      }
      else
      { 
         // Could be anything, e.g database exception - so show generic msg.
         ModelState.AddModelError("Error", "Sorry, an error occured saving the Post. Support has been notified. Please try again later.");
      }
   }

   return View(viewModel);
}

, , "" , . Save post.Validate(), -.

, . Save (90% , Entity Framework), .

, , . , HTTP POST .

+7

"" MetaData - , . , , viewmodel:

public class PersonMetaData
{
  [Required] 
  public string Name {get; set;}  

  [Required] 
  public string LastName {get; set;} 
}

[MetadataType(typeof(PersonMetaData))]
public class Person
{
  public string Name {get; set;}  
  public string LastName {get; set;} 
}

[MetadataType(typeof(PersonMetaData))]
public class PersonFormViewModel 
{
  public string Name {get; set;}  
  public string LastName {get; set;} 
}
+4

RPM1984 .

- , 2 - , 3, , , , 3, .., , , , , 3 .

, - , RPM1984: - , , - (, ). 3, View ( ), - -.

+2

, , , . , PersonViewModel , Person Model (, automapper ..) , PersonViewModel . , , .

+1

Typically, your ViewModel will contain a link to your model. ViewModel is necessary only if you need to display additional information in your view that is not available in your model, there is no need to replicate existing data.

0
source

All Articles