C# ASP.NET MVC新手,在包含表单的呈现操作中遇到问题
我已经习惯了ASP.NETWeb表单,正在慢慢学习ASP.NETMVC。 我的网站在主页上有一个小的登录表单。我的自然想法是,这个登录表单在其他地方可能有用,而且它不是主页的主要操作,所以我想将它分离成一个局部视图。因为它与帐户相关,所以我希望登录到我的AccountController而不是HomepageController 登录表单是一个非常基本的强类型局部视图:C# ASP.NET MVC新手,在包含表单的呈现操作中遇到问题,c#,asp.net-mvc,asp.net-mvc-3,model-view-controller,renderaction,C#,Asp.net Mvc,Asp.net Mvc 3,Model View Controller,Renderaction,我已经习惯了ASP.NETWeb表单,正在慢慢学习ASP.NETMVC。 我的网站在主页上有一个小的登录表单。我的自然想法是,这个登录表单在其他地方可能有用,而且它不是主页的主要操作,所以我想将它分离成一个局部视图。因为它与帐户相关,所以我希望登录到我的AccountController而不是HomepageController 登录表单是一个非常基本的强类型局部视图: @model Models.Account.AccountLogin <h2>Login Form<
@model Models.Account.AccountLogin
<h2>Login Form</h2>
@using (Html.BeginForm("_Login", "Account")) {
@Html.ValidationSummary()
<div>
<span>Email address:</span>
@Html.TextBoxFor(x => x.EmailAddress)
</div>
<div>
<span>Password:</span>
@Html.PasswordFor(x => x.Password)
</div>
<div>
<span>Remember me?</span>
@Html.CheckBoxFor(x => x.RememberMe)
</div>
<input type="submit" value="Log In" />
}
</div>
最后,在account controller中:
[HttpGet]
public PartialViewResult _Login()
{
return PartialView();
}
[HttpPost]
public PartialViewResult _Login(AccountLogin loginDetails)
{
// Do something with this
return PartialView();
}
现在,当我加载我的主页时,它看起来不错,并且包含表单。当我单击“登录”按钮时,它会将我带到myurl/Account/\u Login
,其中包含表单,但不在“布局母版页”中,只包含基本的纯HTML,当我单击“登录”时,它根本不做任何事情
我很确定我刚刚错过了我应该在这里做的一些基本方面,有人能给我指出正确的方向吗?这是因为你返回的是一个局部视图,它去掉了母版页,只返回了主要内容。通常,以下划线开头的操作用于局部(例如,在页面的某一部分使用ajaxing,但不用于整页)。听起来你想要一个完整的行动,而不是一个局部的行动,所以
[HttpPost]
public ActionResult Login(AccountLogin loginDetails)
{
// Do something with this
return View();
}
这是因为您返回的是一个局部视图,它去掉了母版页,只返回主要内容。通常,以下划线开头的操作用于局部(例如,在页面的某一部分使用ajaxing,但不用于整页)。听起来你想要一个完整的行动,而不是一个局部的行动,所以
[HttpPost]
public ActionResult Login(AccountLogin loginDetails)
{
// Do something with this
return View();
}
这里的问题是,你正在做一个完整的页面回发 你有两个选择,真的 首先,您可以使用整页回发,然后调用
Html.Partial
来显示您的部分内容
差不多
[HttpGet]
public ActionResult Login()
{
return View();//this typically returns the view found at Account/Index.cshtml
}
然后沿直线创建一个视图
@{
ViewBag.Title = "Index";
}
<h2>Title</h2>
@Html.Partial("PartialNameGoesHere")
创建视图
@{
ViewBag.Title = "Index";
}
<h2>Home</h2>
<div>
<p>Hey welcome to my pretty awesome page!</p>
</div>
<a href="#" class="my-login-link">Show me the login!</a>
<div id="container">
</div>
在这种情况下,页面正常加载,没有登录部分。当用户单击链接时,将使用AJAX/jQuery调用控制器
帐户
上的登录
。这将返回PartialView的HTML,然后您可以使用Success
处理程序中的jQuery将其添加到页面中。这里的问题是您正在进行完整页面的回发
你有两个选择,真的
首先,您可以使用整页回发,然后调用Html.Partial
来显示您的部分内容
差不多
[HttpGet]
public ActionResult Login()
{
return View();//this typically returns the view found at Account/Index.cshtml
}
然后沿直线创建一个视图
@{
ViewBag.Title = "Index";
}
<h2>Title</h2>
@Html.Partial("PartialNameGoesHere")
创建视图
@{
ViewBag.Title = "Index";
}
<h2>Home</h2>
<div>
<p>Hey welcome to my pretty awesome page!</p>
</div>
<a href="#" class="my-login-link">Show me the login!</a>
<div id="container">
</div>
在这种情况下,页面正常加载,没有登录部分。当用户单击链接时,将使用AJAX/jQuery调用控制器帐户
上的登录
。这将返回PartialView的HTML,然后可以使用Success
处理程序中的jQuery将其添加到页面中