, . , , :
1- . , /. , .
2- , ASP.NET MVC Framework. , Binders?
, , "" Model Binders. , . . , . . "". :
public ActionResult Save(DocumentViewModel viewModel)
{
if (!ModelState.IsValid)
{
viewModel.Categories = _repository.GetAll();
return View(viewModel);
}
}
I believe that initializing categories here is ugly and smells like code. What if you had several properties that needed to be populated from the database? What if you had more than one action with a DocumentViewModel as an argument? You will have to repeat this ugly step again and again. The best approach is to fill out all the model properties using the Binder module and pass it to the controller. Thus, the object that is transferred to the controller is in a “consistent” state.
source
share