We still want to use the base classes method by calling base.BindModel. In PersonnelModelBinder I have overridden the BindModel method which is what is called by the MVC system when you want to deserialize an object from the request data. To create a custom binder, I create a class that derives from .Ĭopy Code .Binders.Add( typeof(Personnel), new PersonnelModelBinder()) We can create one that is aware of the fact that Address is allowed to be null and will adjust accordingly. In ASP.NET MVC, you are allowed to create your own custom model binders. The binder is creating a instance of the Address object, which then means when saving occurs, there will be a blank record inserted into the database. So now we are able to get passed the validation but there is still one problem.**When I say Request key, I mean the key value in the collection of where the DefaultModelBinder looks for incoming information: Request.Form, the URL, Request.QueryString, etc. So now, because the binder is no longer trying to put an empty string into the integer Address.ID, no errors are registered and validation passes. If no key is found, then Personnel.Address will be null. When binding is done, if it sees any Request key named " Address.whatever" it will create a new Address object and put it into Personnel.Address. A thing to note is that the DefaultModelBinder will still create an instance of Address on the Personnel object because the Street input element is there. Now, if Address is null, no markup will be created for the input element for Address.ID. I want to be able to check if the model is null and if so, not create any markup. So we could create a HtmlHelper extension method that mimics what HiddenFor does. If the Request does not contain an entry for Address.ID then the binder will not try to convert the value into an integer. A way to do this is to not have an input element for Address.ID. What if we prevented the initial condition that is causing the problem in the first place, that being a empty string cannot be converted into a integer for Address.ID.Perhaps this would be acceptable in some situations. If the user saves, yes all the data from the request will be valid and the model binder won't create an error, but then a blank Address record will be inserted into the database. I guess this is ok, but it is not considering the fact that the admin user might not enter anything in the address fields which indicates that the Person does not have an address. Public ActionResult EditPersonnel(Personnel model) I have a partial view that is used for the Address Type which looks something like this: So Address may be null but yet the UI input elements still need to be present in the page. The UI or View still needs to still show the input elements for the address model because at any time the admin user can then just put in the person's address OR they can leave it blank if they don't intend to provide address information. Let's also assume that if Address is not null, then whatever mechanism you are using for persistence will insert a record in the database if ID =0, else it will do an update. The key thing to realize is that it is possible for the Address property of Personnel to be null because not all personnel had their address information entered and so there will be no record in the database for it. I won't show the code but, let's say there is some mechanism (entity framework or whatever) to retrieve an instance of a Personnel object by ID. Realize that there is a one to one relationship between Person and Address, and that Person.AddressId is nullable. So let's say I have 2 domain models as such. There are input elements for first name, last name, street address, etc. I have an admin screen where I am editing the information for Personnel. I found myself running into a specific situation, Perhaps others have too? I'll assume you are pretty familiar with ASP.NET MVC. Having models that can possibly be null creates some complications in ASP.NET MVC. But at the same time, you have to show the view of the address because the admin user could enter the address at any time. It is possible that a person doesn't have an address for whatever reason, or you just need to enter their name in the system without the address. Well what happens if one of these domain models can be null. You bind this to a view that is used for admin purposes like changing the person's name or address. For example, a Personnel view model that has a Person object and an Address object. Have you ever had a view model composed of several domain models.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |