That text area of nullness
I have a strange problem and it's been frustrating 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
FormCollection formCollection
instead of UserInfo userInfo
. [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.
链接地址: http://www.djcxy.com/p/9138.html下一篇: 那个文本区域是无效的