请告诉我如何在ASP.NET MVC4应用程序中创建更新面板。我查看了许多博客...但是找不到任何有用的方法。
这是我的视图
我该如何在同一视图中分离这些操作?
![enter image description here](https://istack.dev59.com/fRRDH.webp)
在ASP.NET MVC中并没有真正的Update panel。在人们意识到使用手写jQuery ajax进行局部页面更新更好之前,它曾经存在于ASP.NET Web表单开发世界中。
您可以使用jQuery ajax方法将表单数据发布到操作方法,并根据需要对页面进行部分更新。您还可以考虑根据需要使用Partial View(返回页面的一部分)。
在您的情况下,您可以为“登录”和“注册”创建两个Partial View,并将它们包含在主视图中,以使您的代码更具可重用性。
<h1>Login or Register</h1>
<div>
@Html.Partial("Login")
</div>
<div>
@Html.Partial("Register")
</div>
你的两个面板没有让用户在它们之间切换,所以我猜测你有一个“介绍”视图,其中有选项可以选择登录或注册。对吗?如果是这样的话,就没有必要使用Javascript/Ajax在客户端进行面板切换了。你的“介绍”视图可以将参数传递给控制器操作,定义它是否需要返回登录或注册视图。
例如:
// RouteConfig.cs
routes.MapRoute(
name: null,
url: "login-register/{action}",
defaults: new { controller = "Authentication"},
constraints: new { action = @"(SignIn|Register)" }
);
// AuthenticationController.cs
public ActionResult SignIn()
{
...
return View(); // Will return the Authentication\SignIn.cshtml view
}
public ActionResult Register()
{
...
return View(); // Will return the Authentication\Register.cshtml view
}