如何处理MVC 4(ASPX)中的事件

时间:2016-03-02 13:36:08

标签: asp.net-mvc-4

我是MVC的新手,用VSX选择在VS12 MVC4中创建了应用程序。我有设计母版页。创建一个控制器,然后从控制器查看 - >添加视图 使用嵌套创建的母版页。 我的视图代码如下(仅显示所需代码)

<asp:Table runat ="server"  HorizontalAlign ="Center" >
        <asp:TableRow >
            <asp:TableCell >                    
                <dx:ASPxButton ID="btnlogin" runat="server" Text="Login"></dx:ASPxButton>
            </asp:TableCell>
             <asp:TableCell >
                <dx:ASPxButton ID="btnCancel" runat="server" Text="Cancel"></dx:ASPxButton>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>

登录成功后如何重定向到另一个页面?

1 个答案:

答案 0 :(得分:1)

d0812!

实际上,在您的家庭控制器中,方法索引(默认情况下应该首先调用客户端)这样的内容将是:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

对于您的应用程序,它意味着以下内容:如果客户端调用http(s):// yourapp /或http(s):// yourapp / home,则此方法将调用。 ActionResult通常是服务器响应。

接下来,客户端将从文件夹/(root)/ Views / Home /.

接收名称为Index的文件

但你也可以这样做:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("myViewName");
    }
}

然后您的客户端将在同一文件夹中找到名为myViewName的文件。

因此,您可以检查您的客户的身份:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        if (User.Identity.IsAuthenticated)
        {
            return View("myViewName");
        }
        return View();
    }
}

接下来,您无法使用属性

实现客户端授权的POST方法
    [HttpPost] 
    public ActionResult Login(LoginModel model)
        {
             //TODO: implement
             //note: you can redirect the user here 
             //as described above
        }

其中LoginModel只是可序列化的类:

public class LoginModel
{
    public string Login { get; set; }
    public string Password { get; set; }
}

或开始使用something like owin2

我希望,它会对你有帮助。