【问题标题】:Show text from input after submit提交后显示输入中的文本
【发布时间】:2011-11-02 11:54:55
【问题描述】:

喂!

我想在页面开头提交后显示输入中的文本

我的代码:

@{
    ViewBag.Title = "Login";
}
@{
    if (ViewData["uname"]!=null )
    {
            <div>
            Username: @ViewData["uname"] //The Text from the input should be displayed here after submit
            @Session["uname"] = @ViewData["uname"]
            </div>
    }
}
<h2>Login</h2>
<h4>Username:</h4>
<input type="text" name="uname" value="" /> //Input
<button />

当我按下按钮时,没有任何反应。输入中的文本应该出现在大约 h。按钮的标准类型是提交。

【问题讨论】:

  • 你能解释一下你到底想做什么吗?您希望文本显示在哪里?

标签: asp.net asp.net-mvc-3 razor


【解决方案1】:

试试这个:

@{ 
    ViewBag.Title = "Login"; 
} 
@{ 
    if (ViewData["uname"]!=null ) 
    { 
            <div> 
            Username: @ViewData["uname"] 
            @Session["uname"] = @ViewData["uname"] 
            </div> 
    } 
} 
<h2>Login</h2> 

@using (Html.BeginForm())
{
    <h4>Username:</h4> 
    <input type="text" name="uname" value="" /> 
    <input type="submit" value="Submit" /> 
}

这使用一个 HtmlHelper 来创建 &lt;form /&gt; 周围的标签,所以提交它应该发布。

【讨论】:

  • @test123123 你在相应的控制器中有一个 POST(带有属性[HttpPost])动作来处理这个帖子吗?
  • Controller: public class UserController : Controller { // // GET: /User/ public ActionResult Index() { return View(); } public ActionResult Login() { return View(); } }
猜你喜欢
  • 1970-01-01
  • 2014-01-10
  • 2013-11-22
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 2013-09-22
  • 2021-01-08
  • 1970-01-01
相关资源
最近更新 更多