2011-09-26 17 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(); 

巴姆!它再次運作。這從來沒有發生過,因爲我以前做過沒有任何問題。我很高興它是固定的。我們都會犯錯。