如何在MVC 5帐户登录操作中添加参数?

时间:2018-01-18 02:38:38

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

MVC中Account控制器中的登录操作有两个参数:

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        LoginStatus status = new LoginStatus();

        if (!ModelState.IsValid)
        {
            return View(model);
        } 
        ....

我想加一个。像...

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl, string anotherOne)
    {
        LoginStatus status = new LoginStatus();

        if (!ModelState.IsValid)
        {
            return View(model);
        } 
        ....

该动作的帖子是一个Html.BeginForm,就像......

    @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))  

当我尝试在该帖子中添加另一个参数时,例如......

    @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, new { anotherOne = "testdata" }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))

......我猜这打破了路由?我收到“处理您的请求时出错”。我猜我没有办法处理那个额外的参数?我是朝着正确的方向前进还是有其他方式我应该将附加参数传递给Login操作?

1 个答案:

答案 0 :(得分:0)

您需要将BeginForm()方法的第3个参数中的所有路径/查询字符串值添加为object

@using (Html.BeginForm(
    "Login",
    "Account", 
    new { ReturnUrl = ViewBag.ReturnUrl, anotherOne = "testdata" }, 
    FormMethod.Post, 
    new { @class = "form-horizontal", role = "form" })
)