我有一个奇怪的问题,过去几个小时让我很沮丧.我似乎找不到任何相关的东西; 也许我不够具体,因为我不确定如何正确地说出来,或者这是一个奇怪的独特问题.
有一个用户填写的表单来更新他们的帐户信息,一切正常,除了一个文本区域.这个文本区域(被绑定到的属性Comments的UserInfo,一旦表单被提交)值变为零.该Comments属性是唯一的null属性.
何时发生
A)没有现有值,用户输入值,属性为空.
B)现有价值,用户做/不改变什么/什么,属性为空.
我只会包含相关代码以保持简洁.希望这就够了.
控制器动作
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.
}
Run Code Online (Sandbox Code Playgroud)
Razor查看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" })
Run Code Online (Sandbox Code Playgroud)
UserInfo 模型
[Validator(typeof(UserInfoValidator))]
public class UserInfo
{
public string Comments { get;set; }
}
Run Code Online (Sandbox Code Playgroud)
是的,我在模型上使用FluentValidation.我删除了它,看它是不是原因,但事实并非如此.
我试过的事情
FormCollection formCollection而不是UserInfo userInfo.[Bind(Prefix = "")]过UserInfo userInfo.这并没有改变任何事情.令我感到沮丧的是,我不得不问:这到底是怎么回事?难道我做错了什么?我必须忽视一些事情.页面上还有另一个文本区域可以正常工作.它只是文本区域,Comments无论条件如何,它始终返回空值.
表格的包装方式如下:
Html.BeginWindow();
Html.BeginForm("edit_information", "user", FormMethod.Post, new { id = "profile" });
<!-- other stuff goes in between here -->
Html.EndForm();
Html.EndWindow();
Run Code Online (Sandbox Code Playgroud)
Html.BeginWindow()生成一个包裹在表单周围的表格(一个窗口)。这显然导致部分表单无法正确发布。
变成:
Html.BeginForm("edit_information", "user", FormMethod.Post, new { id = "profile" });
Html.BeginWindow();
<!-- other stuff goes in between here -->
Html.EndWindow();
Html.EndForm();
Run Code Online (Sandbox Code Playgroud)
嘭!它又起作用了。我从来没有想到过这一点,因为我以前做过,没有任何问题。我很高兴它被修复了。我们都会犯错。
| 归档时间: |
|
| 查看次数: |
289 次 |
| 最近记录: |