开发者

That text area of nullness

开发者 https://www.devze.com 2023-04-07 18:50 出处:网络
I have a strange problem and it\'s been fru开发者_如何学JAVAstrating me for the past few hours. I can\'t seem to find anything related; perhaps I\'m not being specific enough, as I\'m not sure how to

I have a strange problem and it's been fru开发者_如何学JAVAstrating me for the past few hours. I can't seem to find anything related; perhaps I'm not being specific enough, as I'm not sure how to word it correctly, or it's a strangely unique problem.

There's a form a user fills in to update their account information, everything works as it should, except for one text area. This text areas' (which is bound to the property Comments of UserInfo) value becomes null once the form is POSTed. The Comments property is the only property which is null.

When It Occurs

A) No existing value, user inputs a value, property is null.

B) Existing value, user does/doesn't change something/anything, property is null.

I'll only include the relevant code to keep things clean and simple. Hopefully it's 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 do use FluentValidation on the model. I removed it to see if it was the cause, but it wasn't.

Things I've Tried

  • On the POST action, I've used FormCollection formCollection instead of UserInfo userInfo.
  • Threw an exception on the POST action to prove the value becomes null when posted.
  • Created a new property with a different name.
  • Manually gave the property a value before returning the view. The value became null when it was posted.
  • Manually gave the property a value in the POST action to prove it wasn't the DB or SQL. This worked.
  • Removed the Fluent Validation attribute from the model (as said above).
  • Used [Bind(Prefix = "")] before UserInfo userInfo. This didn't change anything.

It's frustrated me to the point where I have to ask: What the hell is going? Am I doing something wrong? I must be overlooking something. There is another text area on the page which works as it should. It's just the text area for Comments which always returns null values regardless of the conditions.


The form was being wrapped like so:

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

Html.BeginWindow() generates a table (a window) which is wrapped around the form. This had obviously caused parts of the form not to be POSTed properly.

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. This never occurred to me as I've done it before without any problems. I'm glad it's fixed. We all make mistakes.

0

精彩评论

暂无评论...
验证码 换一张
取 消