I'm making a Web Page with MVC and I would like to know if it's possible to set a field as nullable, I mean, the opposite of Required, because I want it to be an optional field.
Thank you.
All object types can be nullable, i.e. optional, for value types you can use either the type? notation or explicitly with Nullable<type>. If you don't provide a value to the ModelBinder for that property then it will receive NULL.
If you omit the [Required] attribute on the optional field in the model this will have the desired effect.
Related
After i populate some form(asp.net mvc) and press submit button after that form values are the same as before submit the form and i do not repopulate input fields from backend code. Can explain me how values are persisted cross request(and that values are not repopulated)?
Assuming you are asking about how MVC moder binder works(parsing posted data on the server), the default Model binder, which was designed to effectively bind most model types, does a relatively simple and recursive logic for each property of the target model.
1 It determines whether the property is a simple type or complex type by checking the property name are registered as a prefix. The prefix pattern is [ParentProperty].[Property]
2 Get the ValueProviderResult from the registered value providers for the property’s name.
3 If the property is a simple type, it tries to convert the property using the properties TypeConverter from the source value of type string to the from the source value of type string to the target type
4 Otherwise, the property is a complex type, so perform a recursive binding.
If you try to see the posted value the browser sends to the server, you could understand how to form data is sent, and get the idea. For reference, consult this link https://msdn.microsoft.com/en-us/magazine/hh781022.aspx
I have a breeze Entity that has a data property that is a complex object itself. when creating the entity breeze creates a complex object with the default values for that property. I want to make it null, is it posible????
Thanks in advance!!!
Tony
No, a complex property cannot be null, because it is conceptually a value type. We also wanted to make sure that you could always navigation thru a complex type. i.e. that you never have to worry about null checking a complex property path. For example: aCompany.Address.City where the 'Address' property is a complex type will never throw an exception because 'Address' can never be null.
What you can do is set all of the properties within a complex type to null ( or default values), and write logic to handle a complex object that matches these characteristics.
Hope this makes sense.
Just need some clarification.
If I have a form with fields that should not be modified by the user then I need to use the HiddenFor helper to pass those values through otherwise those values will be missing and Model Binding will ensure Nulls are stored for those Field Values.
Of course this seems over zealous of the Model Binder, and one would think that if a field did not exist at all in the View(response stream) then that field would be left untouched. Obviously an EditFor field with a value of "" is different and that value should be null or "".
The only other approach I have come across is to use objects mappers such as Automapper to ensure nulls are ignored.
At the moment I will just use hidden fields to pass the values through.
Thoughts on the above appreciated or rather what is the recommended method for dealing with this issue?
Thanks.
I've been making my viewmodel properties nullable for quite some time now. My reasoning is that when a validation occurs, I don't want default values inserted into the fields that the user left empty, but are required.
I mark my required fields with required, for course, but this got me to thinking that i'm losing a great deal of fidelity in the object model by doing this.
Of course my domain classes are only nullable when they can actually be null.
Should my viewmodel properties be nullable when the domain model requires them?
Should my viewmodel properties be nullable when the domain model requires them?
Yes, they should in order to properly perform validation on the view model. When you ensure that the view model is valid and map this view model back to the actual domain model in your mapping layer you will be certain that a value would be provided for this property.
PLEASE NOTE: I've answered my own question with a link to an answer to a similar question. I'll accept that answer once I'm allowed to (unless anyone comes up with a better answer meantime).
I have a database column defined as NVARCHAR(1000) NOT NULL DEFAULT(N'') - in other words, a non-nullable text column with a default value of blank.
I have a model class generated by the Linq-to-SQL Classes designer, which correctly identifies the property as not nullable.
I have a TextAreaFor in my view for that property. I'm using UpdateModel in my controller to fetch the value from the form and populate the model object.
If I view the web page and leave the text area blank, UpdateModel insists on setting the property to NULL instead of empty string. (Even if I set the value to blank in code prior to calling UpdateModel, it still overwrites that with NULL). Which, of course, causes the subsequent database update to fail.
I could check all such properties for NULL after calling UpdateModel, but that seems ridiculous - surely there must be a better way?
Please don't tell me I need a custom model binder for such a simple scenario...!
Might be duplicate or something in the line of this:
MVC binding form data problem
I fear custom model binder will be necessary. ;)
You might want to use a partial class implementation of your entity that implements the on property changed handler for that particular property. When you detect that the property has been changed to NULL, simply change it to string.Empty. That way anytime NULL is assigned to the property it gets reset to the empty string.