Skip to main content
 首页 » 编程设计

c#之将模型从局部 View 传递到 Controller

2024年08月12日51powertoolsteam

所以我的局部 View 看起来像 -

@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 View 的一部分,但我把它变成了局部 View 。原始 View 被包裹在“Html.BeginForm(..)”中,但我显然不能那样做。(它导致表单内有表单)

如何让它通过传递声明的@model 来调用需要模型的帐户 Controller 的 ForgotPassword 方法?

@Html.TextBoxFor(m => m.Email, new { @class = "form-control"}) 生成的 HTML:

<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 指出的那样,需要确保字段在局部 View 中是唯一标识的。该帖子有两次电子邮件。

请您参考如下方法:

@ErikPhilips How would I use jQuery to change the form's destination? :)

我会将操作链接更改为按钮:

@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> 

J查询:

$(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(); 
  }); 
}); 

这个我没有测试过,应该和你想要的很接近。