Friday, 15 April 2011

entity framework - ASP.NET MVC - How to handle a string in a model that does not allow nulls? -



entity framework - ASP.NET MVC - How to handle a string in a model that does not allow nulls? -

so i'm bit new asp.net , mvc. i've got asp.net mvc5 application, using entity framework 6. i've generated models sql server database, , far using vanilla index/create/details/edit constructs.

in models, various fields marked "allow nulls" , others aren't. while creating new entry, type is, say, int left null handled nicely modelstate.isvalid check , @html.validationmessagefor messages.

on 1 entry test, however, received dbentityvalidationexception. used try/catch this question find out 1 of string (varchar) fields left blank.

i assuming because string class allows nulls, int not (unless declared nullable<type> in model) controller/model doesn't flag invalid.

what easiest way handle this? there way decorate string property in model gets checked well? or need go far attempting save, catching exception, , manually handling validation messages?

thanks

it possible decorate property required-attribute. mvc wil show error message when user posting form while property empty.

public class testclassmodel { [required] public string requiredstring { get; set; } }

asp.net entity-framework asp.net-mvc-5 .net-4.5

No comments:

Post a Comment