asp.net mvc 3 - MVC3: Why won't my ModelError propagate to my view? -


I am working on a .NET MVC3 app. When I get an error in my controller, the error never expires in my view, why?

My verb method looks like this:

  [http post] Create public action roles (userproof model) {if (ModelStat.IsAValid) {return view (model) ; } Try {DependencyResolver.Current.GetService & lt; IUserProfileService & gt; (). CreateUser (model.Email, / * Username for the ultimate map; We use email as the user name * / model, password, model, email, model first name, model last name, model phone, model Remember Fax, Model;); } Catch (Exception Pre) {ViewData.ModelState.AddModelError (string.Empty, ex); See Return (Model); } Return Redirect Action (Action Subscription); }   

My view is very boring:

  ... @using (HTML.BeginForm ()) {@ HTML.ValidationSummary (true) & lt ; Div & gt; & Lt; Div class = "row" & gt; & Lt; Label & gt; @ Html.labelFor (m = & gt; m.FirstName) & lt; / Labels & gt; @ Html.TextBoxFor (m = & gt; m.FirstName) @ html.ValidationMessageFor (m = & gt; m.FirstName) & lt; / Div & gt; & Lt; Div class = "row" & gt; & Lt; Label & gt; @ Html.labelFor (m = & gt; m.LastName) & lt; / Labels & gt; @ Html.TextBoxFor (m = & gt; m.LastName) @ html.ValidationMessageFor (m = & gt; m.LastName) & lt; / Div & gt; ... etc ... & lt; / Div & gt; }   

If I post a form for the person whose username is already in use (for example, where the email address used for the user's email address is used Made from email), CreateUser () calls throws an exception:

  system. Application exception was caught message = error user is making: User already exists   

We then come into the Cache block, add error to ModelState collection, and return the view . So far, very good - from the investigation of ModelState, there is an error on the 10th element in the collection (19 elements in different model properties in the map).

But my thoughts never show an error! I hope that where I call @ html.ValidationSummary () , but there is only one empty element:

  & lt; Div class = "validation-summary-errors" & gt; & Lt; Ul & gt; & Lt; Li style = "display: none" & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Div & gt;   

It is if I pass true or false @ HTML.ValidationSummary () And whether I name the source of the error when I add to the ModelState collection:

  ViewData.ModelState.AddModelError (string.Empty, ex); ViewData.ModelState.AddModelError ("username", former);   

So, the user posts posts, it is rejected, and it is not displayed again with only an error message, so to fix the problem I do not know.

So, I am trying to understand why my error has not been promoted back and I want to stop the error message and make it more clear, so it is clear. That "user" we are talking about that, he has the email recorded on the form.

Any help appreciated!

OK, I finally got the problem: AddModelError () is the method Two surcharges, one which takes exception, and which takes the string when I pass the entire exception:

  ViewData.ModelState.AddModelError (string.Empty, ex);   

, I have not provided any error in the verification. When I pass the message string of exception:

  ViewData.ModelState.AddModelError (string.Empty, ex.Message);   

Issues the error message correctly.

Now back to our regularly scheduled programming.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

c# - Confused over DLL entry points (entry point not found exception) -