2012-09-11 71 views
0

ASP.Net MVC 3剃鬚刀 我用下面的代碼如何清除文本

 <input name ="button" type="submit" value="AddData" /> 

我讀了服務器端的控制操作方法裏面的模態數據提交頁面。

予讀出的數據是從下面的代碼

@ Html.TextAreaFor(M => m.Letter.Letters,新{ID = 「LetterTextArea」});

提交頁面後,我無法清除文本框中的文本。

如何在提交(單擊按鈕)數據後清除文本。

謝謝 史密斯

+0

可能重複http://stackoverflow.com/questions/1575813/how-to-clear-the-post-data-for-a-textbox-in-an-asp-net-mvc-application –

回答

2

我想你都返回相同的模型視圖回來。你應該做的,而不是重定向到GET行動(Post -Redirect- Get Pattern

[HttpPost] 
public ActionResult Add(UserViewModel model) 
{ 
    if(ModelState.IsValid) 
    { 
    //Save the data, If everything is ok, We will do a Redirect to GET action 
    return RedirectToAction("Add","User"); 
    } 
    return View(model);  
} 

RedirectToAction方法會發出GET請求,並擊中其返回空表格GET操作方法。

public ActionResult Add() 
{ 
    var userViewModel=new UserViewModel(); 
    return View(userViewModel); 
} 

Here是一個很好的教程,爲什麼你應該考慮PRG模式。

0

除了@Shyju答案這是在這種情況下,最好的做法,你可以從ModelState中刪除值,因爲HTML助手將首先使用一個儲存裏面的ModelState,之後在你的模型中的價值:

[HttpPost] 
public ActionResult SomeAction(MyViewModel model) 
{ 
    ModelState.Remove(Letter.Letters); 
    model.Letter.Letters = ""; 
    return View(model); 
}