将模型从局部视图传递到控制器

时间:2014-11-13 18:23:09

标签: c# asp.net-mvc razor asp.net-mvc-5

所以我的部分视图看起来像 -

@model Example.Models.ForgotPasswordViewModel

@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    </div>
</div>
<div style="font-size: 16px; text-align:center; margin-top:10px;">
    @Html.ActionLink("Email Link", "ForgotPassword", "Account")
</div>

作为asp.net开箱即用的ForgotPassword视图的一部分,但我把它变成了局部视图。原始视图包含在&#34; Html.BeginForm(..&#34;,但我显然不能这样做。(它会在表单中生成一个表单)

如何通过传递声明的@model来调用帐户控制器的ForgotPassword方法,该方法需要模型?

由@ Html.TextBoxFor生成的Html(m =&gt; m.Email,新{@class =&#34; form-control&#34;}):

<input class="form-control" id="Email" name="Email" type="text" value="" />
您的AccountController.ForgetPassword(..)的

签名:

//
// POST: /Account/ForgotPassword
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
        {
            // Don't reveal that the user does not exist or is not confirmed
            return View("ForgotPasswordConfirmation");
        }

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
        // Send an email with this link
        string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
        var callbackUrl = Url.Action(
            "ResetPassword", "Account", 
             new { userId = user.Id, code = code }, 
             protocol: Request.Url.Scheme);

        await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
        return View("ForgotPasswordConfirmation");
    }

    // If we got this far, something failed, redisplay form
    return RedirectToAction("Index", "Home");
}

同样,Erik指出,需要确保在局部视图中唯一标识字段。该帖子有两次电子邮件。

2 个答案:

答案 0 :(得分:3)

  

@ErikPhilips如何使用jQuery更改表单的目标? :)

我会将Action链接更改为按钮:

@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    </div>
</div>
<div style="font-size: 16px; text-align:center; margin-top:10px;">
    <input type="button" 
            value="Email Link" 
            class="change-form-url"
            data-url="@Url.Action("ForgotPassword", "Account")"/>
</div>

Jquery的:

$(document).ready(funciton()
{
  $('.change-form-url').on('click', function()
  {
    var url = $(this).data('url');
    var form = $(this).closest('form');
    form.prop('action', url);
    form.submit();
  });
});

我没有测试过,它应该非常接近你想要的。

答案 1 :(得分:0)

BeingForm有一些重载:

@using (Html.BeginForm("ActionName","Controller"))
{
    @Html.TextBox("Name");
    @Html.Password("Password");
    <input type="submit" value="Sign In">
}

将动作名称和控制器名称添加到beginForm调用中,您可以覆盖默认情况下Post的位置。

我建议你在主表单之外调用你的局部视图,这样你就可以在一个页面上有两个表单(不是嵌套的)。然后,您可以将忘记密码模型指向正确的控制器方法。