Asp.net mvc MVC3一个视图中有两个局部视图

Asp.net mvc MVC3一个视图中有两个局部视图,asp.net-mvc,asp.net-mvc-3,razor,Asp.net Mvc,Asp.net Mvc 3,Razor,我正在努力学习MVC 3和Razor,现在我被困在这里3个小时了。这就是我所拥有的 MVC项目是使用默认模板创建的,其中包含帐户注册和模板中的所有好东西。我想做的是在HomeController的索引中同时包含注册页面和登录页面,因此我为Register(_RegisterPartial)和LogOn(_LogOnPartial)创建了一个局部视图。当我进入索引页面时,我会看到注册和登录表单,这很好,但当我尝试登录或注册时,它会进入一个无限循环。 我的家庭控制器看起来像这样 // *******

我正在努力学习MVC 3和Razor,现在我被困在这里3个小时了。这就是我所拥有的

MVC项目是使用默认模板创建的,其中包含帐户注册和模板中的所有好东西。我想做的是在HomeController的索引中同时包含注册页面和登录页面,因此我为Register(_RegisterPartial)和LogOn(_LogOnPartial)创建了一个局部视图。当我进入索引页面时,我会看到注册和登录表单,这很好,但当我尝试登录或注册时,它会进入一个无限循环。

我的家庭控制器看起来像这样

// **************************************
// Registration
// **************************************

public ActionResult DoRegister()
{
    return PartialView("_Register");
}

[HttpPost]
public ActionResult DoRegister(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email, model.UserProfile);

        if (createStatus == MembershipCreateStatus.Success)
        {
            FormsService.SignIn(model.UserName, false); // createPersistentCookie
            return View("Success");
        }
        else
        {
            ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
        }
    }

    // If we got this far, something failed, redisplay form
    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
    return View(model);
}


// **************************************
// Login
// **************************************

public ActionResult DoLogin()
{
    return PartialView("_Login");
}

[HttpPost]
public ActionResult DoLogin(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(model.UserName, model.Password))
        {
            // logged in
            FormsService.SignIn(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl))
            {
                Redirect(returnUrl);
            }
            else
            {
                View("Success");
            }
        }
        else
        {
            // Not logged in
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View("Success");
}
@{
    ViewBag.Title = "Home Page";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@if (Request.IsAuthenticated)
{
    @Html.ActionLink("Log Off", "LogOff", "Account")
}
else
{
    Html.RenderAction("DoLogin");
    Html.RenderAction("DoRegister");
}
我的cshtml如下所示

// **************************************
// Registration
// **************************************

public ActionResult DoRegister()
{
    return PartialView("_Register");
}

[HttpPost]
public ActionResult DoRegister(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email, model.UserProfile);

        if (createStatus == MembershipCreateStatus.Success)
        {
            FormsService.SignIn(model.UserName, false); // createPersistentCookie
            return View("Success");
        }
        else
        {
            ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
        }
    }

    // If we got this far, something failed, redisplay form
    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
    return View(model);
}


// **************************************
// Login
// **************************************

public ActionResult DoLogin()
{
    return PartialView("_Login");
}

[HttpPost]
public ActionResult DoLogin(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(model.UserName, model.Password))
        {
            // logged in
            FormsService.SignIn(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl))
            {
                Redirect(returnUrl);
            }
            else
            {
                View("Success");
            }
        }
        else
        {
            // Not logged in
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View("Success");
}
@{
    ViewBag.Title = "Home Page";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@if (Request.IsAuthenticated)
{
    @Html.ActionLink("Log Off", "LogOff", "Account")
}
else
{
    Html.RenderAction("DoLogin");
    Html.RenderAction("DoRegister");
}
问候,


Ryan

您是否阅读异常消息

A public action method 'Register' was not found on controller 'AudioRage.Controllers.HomeController'
现在看看你发布的
HomeController
代码。你在上面看到注册操作了吗?我不知道

因此,添加一个:

public ActionResult Register()
{
    ...
}
HomeController
中,您有一个名为Register的操作,但该操作只能通过POST谓词访问,因为它用
[HttpPost]
属性修饰:

[HttpPost]
[ActionName("Register")]
public ActionResult Index(RegisterModel model)

因此,您不能在
/Home/Register

上用GET动词调用它,我无法准确地复制您的情况,但我要说的是,您的部分表单发布不正确。查看页面的呈现html并检查表单的发布位置。我的猜测是它们被发布到
索引
操作中。再加上重定向,我认为这就是无限循环的来源

我猜这两个表单呈现的html是相似的,并且发布到相同的操作,即
,因为它们是由HomeController的索引操作呈现的

更改部分表单(
\u Login.cshtml
\u Register.cshtml
)并明确说明要发布到的操作/控制器组合(从MSDN)

我还将
Html.RenderAction
调用更改为

Html.RenderPartial("_Login");
Html.RenderPartial("_Register");

我愿意。他使用了ActionName属性。@Linkgoron,是的,他还使用了
[HttpPost]
属性。这是正确的。他的问题是理解get和post属性,这是一个与您最初陈述的问题不同的问题。@Linkgoron,我已经更新了我的答案以包含这些信息。谢谢您的回复。我修改了上面的代码,但现在它进入了一个无限循环。即使我单击了登录按钮,它也会进入注册帖子。我来自ASP.NET webforms背景,我想了解如何将部分视图的事件连接到代码。因为在MVC中,我们不能有代码隐藏,我如何告诉按钮执行什么操作。因为如果我做Html.RenderPartial(“_Login”);它呈现得很好,但当我点击登录按钮时,什么也没发生。我是否必须为每个呈现该部分视图的控制器编写登录例程?@ryan-我在上面放置的
Html.BeginForm
说明表单需要发布到哪个控制器/操作。一般来说,我认为登录表单和注册表单将始终发布到同一个控制器/操作组合,在这种情况下,
DoLogin
DoRegistration
控制器的
Home
操作。我想你们可能需要用部分视图的代码来更新你们的问题。Html.BeginForm(“DoLogin”,“Home”)解决了这个问题。非常感谢你!