我通过选择(ASP.Net MVC2Web Application)在MVC2中创建了一个应用程序。这提供了一些Home/About控制器/模型/视图。
我另外创建了一个名为Index的模型,如下所示...
namespace MvcApplication1.Models
{
public class Index
{
[DataType(DataType.Text)]
public String Name { get; set; }
}
}以下是我的索引视图
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm())
{%>
<%:Html.TextBoxFor(x=> x.Name) %>
<input type="submit" name="Click here" />
<%} %>
</asp:Content>下面是我的控制器
[HttpPost]
public ActionResult Index(Index Model)
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}问题
当我像下面这样保持索引控制器时。如果我点击提交按钮。这是清除TextBox COntrols。如下所示
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}如果将模型作为参数合并到操作方法中,则不会清除TextBox ...
这种行为的原因是什么?
发布于 2012-10-19 02:19:17
您的控制器应该如下所示,以便用户输入在单击submit按钮后停留在视图中。
public ActionResult Index( )
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
var model = new Index();
return View( model );
}
[HttpPost]
public ActionResult Index(Index model )
{
return View(model);
}https://stackoverflow.com/questions/12960490
复制相似问题