重置textarea的价值形式提交后 [英] Reset the value of textarea after form submission

查看:146
本文介绍了重置textarea的价值形式提交后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我想通过将消息发送到用户ID = 3 / myController的/留言/ 3

  2. 这执行信息()获取]操作,我输入在文本区一些文字,然后点击保存后的形式

  3. 消息()[文章]动作保存更改,重置SomeText的空字符串,并返回到视图中的值。

在这一点上我希望因为我已经把ViewData的文本区是空的[SomeText则会]来的String.Empty

为什么文本区域价值不更新为空字符串后动作后?

下面是操作:

 的[AcceptVerbs(HttpVerbs.Get)
公众的ActionResult消息(INT ID)
{
  计算机[ID] = ID;
  返回查看();
}的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult消息(INT ID,字符串SomeText)
{
  //保存文本数据库
  SaveToDB(ID,SomeText);  //设置SomeText的值清空并返回,以查看
  计算机[SomeText则会] =的String.Empty;
  返回查看();
}

和相应的视图:

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master
    继承=System.Web.Mvc.ViewPage%GT;
< ASP:内容ID =内容1ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
<使用%(Html.BeginForm())
   {%GT;
      &所述;%= Html.Hidden(ID,计算机[ID])%GT;
      <标签=SomeText则会> SomeText:LT; /标签>
      <%= Html.TextArea(SomeText则会,计算机[SomeText则会])%GT;
      <输入类型=提交值=保存/>
<%}%GT;
< / ASP:内容>


解决方案

问题是在的HtmlHelper检索的ModelState值,这是充满了发布的数据。而不是重置的ModelState黑客这一轮,为什么不重定向回[获取]操作。在[发布]操作也可以设置临时状态消息是这样的:

 的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult消息(INT ID,字符串SomeText)
{
  //保存文本数据库
  SaveToDB(ID,SomeText);  TempData的[消息] =消息派;
  返回RedirectToAction(信息);
}

这似乎对我更喜欢正确的行为。

  1. I want to send a message to userID=3 by going to /MyController/Message/3
  2. This executes Message() [get] action, I enter some text in the text area and click on Save to post the form
  3. Message() [post] action saves the changes, resets the value of SomeText to empty string and returns to the view.

At this point I expect the text area to be empty because I have set ViewData["SomeText"] to string.Empty

Why is text area value not updated to empty string after post action?

Here are the actions:

[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();
}

And the corresponding 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>

解决方案

The problem is the HtmlHelper is retrieving the ModelState value, which is filled with the posted data. Rather than hacking round this by resetting the ModelState, why not redirect back to the [get] action. The [post] action could also set a temporary status message like this:

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

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

This seems to me like more correct behaviour.

这篇关于重置textarea的价值形式提交后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆