2011-09-26 29 views
7

我有一个奇怪的问题,它在过去的几个小时里让我很沮丧。我似乎无法找到任何相关的东西;也许我没有足够具体,因为我不知道如何正确地说出它,或者这是一个奇怪的独特问题。该文本区无效

有一种用户填写更新其帐户信息的窗体,除了一个文本区域外,其他所有功能都可以正常工作。一旦表单被发送,该文本区域(其绑定到UserInfo的属性Comments)值就变为空。 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. 
} 

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" }) 

UserInfo型号

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

是的,我在模型上使用FluentValidation。我删除了它,看它是否是原因,但事实并非如此。

事情我已经试过

  • 在POST操作,我用FormCollection formCollection代替UserInfo userInfo
  • 在POST操作中引发异常,以便在发布时证明该值为空。
  • 使用不同的名称创建一个新的属性。
  • 在返回视图之前手动给该属性一个值。该值在发布时变为空值。
  • 手动给POST属性赋值以证明它不是数据库或SQL。这工作。
  • 从模型中删除Fluent Validation属性(如上所述)。
  • UserInfo userInfo之前使用[Bind(Prefix = "")]。这没有改变任何东西。

这让我感到沮丧,因为我不得不问:到底该怎么办?难道我做错了什么?我必须俯视一些东西。在页面上还有另一个文本区域,它应该可以正常工作。这只是Comments的文本区域,无论条件如何,它总是返回空值。

+0

取出类型UserInfo - 只有一个注释参数。它是否填充?表单中有评论(你看到它张贴在Fiddler中)? –

+0

完成。评论存在于表格中(至少我假设它是)。它仍然是空的。我对菲德勒不熟悉,事实上,我从来没有听说过。 –

+1

也许是一个超级愚蠢的问题,但你确定你在这里发布的“评论”部分包含在正确的表单标签内吗?或者,也许您在视图中遇到名称冲突? – Iridio

回答

3

的形式被包裹像这样:

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

Html.BeginWindow()产生被周围的形式缠绕的表(一窗口)。这显然导致部分表单无法正确发送。

更改为:

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

巴姆!它再次运作。这从来没有发生过,因为我以前做过没有任何问题。我很高兴它是固定的。我们都会犯错。