tailieunhanh - Pro ASP.NET MVC Framework phần 8
Tuy nhiên, những tương tác của người dùng lên trang web sẽ được truyền thẳng cho controller mà không thông qua cơ chế postback truyền thống. Với MVC framework, chúng ta còn có thể làm được 1 trang web với Friendly-URL mà không phải mất công code những implement phức tạp như trước đây. MVC Preview 3 Release vừa mới được công bố hồi tháng 5/2008 với nhiều feature mới hứa hẹn nhiều điều hấp dẫn để tìm hiểu cho những lập trình viên như chúng ta | CHAPTER 11 DATA ENTRY 375 This permits an elegant way of unit testing your model binding. Unit tests can run the action method supplying a FormCollection containing test data with no need to supply a mock or fake request context. It s a pleasingly functional style of code meaning that the method acts only on its parameters and doesn t touch external context objects. Dealing with Model-Binding Errors Sometimes users will supply values that can t be assigned to the corresponding model properties such as invalid dates or text for int properties. To understand how the MVC Framework deals with such errors consider the following design goals User-supplied data should never be discarded outright even if it is invalid. The attempted value should be retained so that it can reappear as part of a validation error. When there are multiple errors the system should give feedback about as many errors as it can. This means that model binding cannot bail out when it hits the first problem. Binding errors should not be ignored. The programmer should be guided to recognize when they ve happened and provide recovery code. To comply with the first goal the framework needs a temporary storage area for invalid attempted values. Otherwise since invalid dates can t be assigned to a .NET DateTime property invalid attempted values would be lost. This is why the framework has a temporary storage area known as Modelstate. Modelstate also helps to comply with the second goal each time the model binder tries to apply a value to a property it records the name of the property the incoming attempted value always as a string and any errors caused by the assignment. Finally to comply with the third goal if Modelstate has recorded any errors then UpdateModel finishes by throwing an InvalidOperationException saying The model of type typename was not successfully updated. So if binding errors are a possibility you should catch and deal with the exception for example public ActionResult RegisterMember .
đang nạp các trang xem trước