Elv*_*dov 3 asp.net-mvc updatepanel asp.net-mvc-4
请解释我如何在ASP.NET MVC4应用程序中创建更新面板.我寻找了很多博客......但找不到任何有用的方法.这是我的观点

我如何在同一视图中分离这些行为?
ASP.NET MVC中并不存在更新面板.在人们实际意识到最好使用手写的jQuery ajax进行部分页面更新之前,它曾经存在于ASP.NET Web表单开发世界中.
您可以使用jQuery ajax方法将表单数据发布到操作方法,并根据需要对页面进行部分页面更新.您还可以根据需要考虑使用部分视图(以返回页面的一部分).
在您的情况下,您可以为登录和注册创建2个部分视图,并在主视图中包含这些视图,以使您的代码更具可重用性.
<h1>Login or Register</h1>
<div>
@Html.Partial("Login")
</div>
<div>
@Html.Partial("Register")
</div>
Run Code Online (Sandbox Code Playgroud)
您的两个面板不允许用户在一个或另一个之间切换,因此我假设您有一个"简介"视图,其中包含登录或注册选项.对?在这种情况下,不需要使用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
}
Run Code Online (Sandbox Code Playgroud)