2009-04-25 104 views
27
  1. 我想發送消息到用戶ID = 3,I輸入在一些文本文本區域並單擊保存以發佈表單
  2. Message()[post]操作保存更改,將SomeText的值重置爲空字符串並返回到視圖。

在這一點上我希望因爲我已經把計算機[「SomeText」則會]到的String.Empty重置textarea的值

爲什麼文本區域價值不更新爲空字符串後動作之後文本區是空的?

這裏是動作:

[AcceptVerbs(HttpVerbs.Get)] 
public ActionResult Message(int ID) 
{ 
    ViewData["ID"] = ID; 
    return View(); 
} 

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Message(int ID, string SomeText) 
{ 
    // save Text to database 
    SaveToDB(ID, SomeText); 

    // set the value of SomeText to empty and return to view 
    ViewData["SomeText"] = string.Empty; 
    return View(); 
} 

以及相應的視圖:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 
<% using (Html.BeginForm()) 
    { %> 
     <%= Html.Hidden("ID", ViewData["ID"])%> 
     <label for="SomeText">SomeText:</label> 
     <%= Html.TextArea("SomeText", ViewData["SomeText"]) %> 
     <input type="submit" value="Save" /> 
<% } %> 
</asp:Content> 

回答

38

問題是是的HtmlHelper檢索的ModelState值,這是充滿發佈的數據。而不是通過重置模型狀態來解決這個問題,爲什麼不重新指向[get]操作。 [post]動作還可以設置如下臨時狀態消息:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Message(int ID, string SomeText) 
{ 
    // save Text to database 
    SaveToDB(ID, SomeText); 

    TempData["message"] = "Message sent"; 
    return RedirectToAction("Message"); 
} 

在我看來,這似乎更正確的行爲。

0

有沒有可能是模型的狀態已經與錯誤更新?我相信,如果模型狀態無效,它會從模型狀態而不是從視圖數據或模型中提取已嘗試的值。

編輯: 我包括source code從下面的文本區域的HtmlHelper擴展的相關章節。在我看來,它完全符合我的預期 - 如果存在模型錯誤,它將從模型狀態中提取值,否則將使用ViewData中的值。請注意,在您的Post方法中,「SomeText」鍵在您設置之前不應該存在,即它不會從響應GET的代碼版本繼承。

由於您明確向ViewData提供值,因此useViewData應該爲false,除非在模型狀態中設置了錯誤,否則attemptedValue應該爲false。

// If there are any errors for a named field, we add the css attribute. 
    ModelState modelState; 
    if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState)) { 
     if (modelState.Errors.Count > 0) { 
      tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName); 
     } 
    } 

    // The first newline is always trimmed when a TextArea is rendered, so we add an extra one 
    // in case the value being rendered is something like "\r\nHello". 
    // The attempted value receives precedence over the explicitly supplied value parameter. 
    string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string)); 
    tagBuilder.SetInnerText(Environment.NewLine + (attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : value))); 
    return tagBuilder.ToString(TagRenderMode.Normal); 
+0

我不認爲有任何錯誤。我以另一種形式出現了同樣的問題。我一直在撓撓我的頭幾天,但我無法找到爲什麼會發生這種情況。當您無法控制發佈後的表單中顯示的內容時,這非常煩人。我想知道我是否在做任何不尋常的事情或錯誤的事情。 – xraminx 2009-04-25 00:25:54

0

這是一個客戶行爲。我會推薦使用JavaScript。如果您使用JQuery,你可以做這樣的:

<script type="text/javascript"> 
$(function(){ $("#SomeText").val("");}); 
</script> 

我不使用JavaScript了,但我相信,在常規JS,它是這樣的:

document.getElementById("SomeText").value = ""; 

(你會做負載的事件之一。

<body onload="..."> 

希望這有助於。

+0

我不這麼認爲。視圖在被推送到我的瀏覽器之前在服務器端呈現。 – xraminx 2009-04-25 00:28:26

+0

我同意,但我在想快速修復。不太清楚爲什麼它會像那樣回來。它在回帖中總是這麼做,認爲這是一個功能。 – 2009-04-25 03:56:25

0

我相當肯定textarea裏抓ŧ由於ViewData [「SomeText」]爲空,所以他從Request.Form中引用該值。

+0

所以你建議正確的做法是: Request.Form [「SomeMessage」] = string.Empty; – xraminx 2009-04-25 00:49:05

+0

否 - Request.Form不被HtmlHelper擴展使用。 – tvanfosson 2009-04-25 00:54:07

+0

我不會那麼說。您可以命名查詢字符串參數標題,並使用名稱標題將文本框命名,並且文本框將具有查詢字符串的值。而且他們確實從Request.Form中獲取值,否則它將如何獲取值以填充表單或在表單發佈後返回結果。 – 2009-04-25 01:01:53

7

html助手從ModelState中讀取值。並沒有優雅的方式來覆蓋這種行爲。

但是,如果你SaveToDB(ID, SomeText)後添加此行,它應該工作:

ModelState["SomeText"].Value = 
    new ValueProviderResult("", "", CultureInfo.CurrentCulture); 
68

的問題是,你的ModelState重新充滿公佈值。

你可以做的是明確它具有郵政屬性的操作:

ModelState.Clear(); 
1

我什麼都試過,但只有當我做了一件這樣的工作:

ModelState.Clear(); 
//This will clear the address that was submited 
viewModel.Address = new Address(); 
viewModel.Message = "Dados salvos com sucesso!"; 
return View("Addresses", ReturnViewModel(viewModel)); 

希望這有助於。

1

而不是使用清除整個模型狀態的ModelState.Clear(),如果需要,可以執行ModelState.Remove(「SomeText」)。或者在沒有htmlhelper擴展的情況下渲染輸入。 它們旨在從ModelState中取值而不是Model(或viewdata)。

0

做s.th.像這樣:

地址:

ModelState.Clear(); 

提交按鈕操作方法的return語句之前。適用於我。它可以爲你工作。