This text area is nullness

I have a strange problem and this has been frustrating me for the past few hours. I do not seem to see anything related; maybe I’m not specific enough, since I’m not sure how to formulate this correctly, or this is a strangely unique problem.

There is a form that the user fills out to update their account information, everything works as it should, with the exception of one text area. These text areas' (which are tied to the Comments of UserInfo property) become null after the POSTED form. The Comments property is the only property that is null.

When does this happen
A) There is no existing value, the user enters a value, the property is null.
B) The existing value, the user does / does not change anything / nothing, the property is null.

I will include only the appropriate code so that everything is clean and simple. I hope this is enough.

Controller Actions

 public ActionResult Edit_Information(long id) { // Get user info from the database. // Return the view with the user info from the DB etc. } [HttpPost] public ActionResult Edit_Information(long id, UserInfo userInfo) { if (!this.ModelState.IsValid) { // Invalid return View(userInfo); } // Update the information in the DB. // Redirect the user back to their account. } 

Razor View HTML

 <div style="width: 700px; margin-left: auto; margin-right: auto; text-align: left"> @Html.ValidationMessageFor(x => x.Comments) </div> @Html.Partial("~/Views/Shared/_EditorSmiles.cshtml") @Html.TextAreaFor(x => x.Comments, new { @class = "EditorArea profile-comments" }) 

UserInfo Model

 [Validator(typeof(UserInfoValidator))] public class UserInfo { public string Comments { get;set; } } 

Yes, I am using FluentValidation for the model. I deleted it to make sure that this is the reason, but it is not.

Things i tried

  • In the POST action, I used FormCollection formCollection instead of UserInfo userInfo .
  • Threw an exception to the POST action to prove that the value becomes zero when published.
  • A new property with a different name has been created.
  • Manually assigned the property value before returning the view. The value was null when it was published.
  • Manually assigned the property value in the POST action to prove that it is not a database or SQL. It worked.
  • Removed the Fluent Validation attribute from the model (as described above).
  • Used by [Bind(Prefix = "")] before UserInfo userInfo . This has not changed anything.

This upset me to such an extent that I have to ask: “What the hell is going on? Am I doing something wrong? I have to ignore something. There is another text area on the page that works as it should. It's just The text area for Comments , which always returns zero values ​​regardless of conditions.

+7
source share
1 answer

The form was wrapped as follows:

 Html.BeginWindow(); Html.BeginForm("edit_information", "user", FormMethod.Post, new { id = "profile" }); <!-- other stuff goes in between here --> Html.EndForm(); Html.EndWindow(); 

Html.BeginWindow() creates a table (window) that is wrapped around the form. This clearly caused parts of the form to not be submitted correctly.

Changed to:

 Html.BeginForm("edit_information", "user", FormMethod.Post, new { id = "profile" }); Html.BeginWindow(); <!-- other stuff goes in between here --> Html.EndWindow(); Html.EndForm(); 

Bam! It worked again. It never crossed my mind, since I did it without any problems. I am glad that this is fixed. We all make mistakes.

+3
source

All Articles