第二个表单操作,如何从Html.TextBoxFor获取值

时间:2016-05-26 20:55:57

标签: asp.net-mvc-4 c#-4.0

已经查看了get value from Html.TextBoxFor和其他几个地方。

我想将UserName传递给丢失的密码屏幕。我添加了name =“bjh”和id =“bjh”作为其他地方不会使用的名称,最初我使用了Username我模型中的值(m.Username)。 正常登录工作,值传递,但我忘记密码链接@ LangStrings.LostPassword没有获取用户名值 @ Html.TextBoxFor(m => m.Username,new {style =“width:100%”,id =“bjh”,name =“bjh”})

登录视图

@model HelpDesk.Models.LoginUserModel

@using (Html.BeginForm("Login", "User", new { ReturnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post))
{
<table class="grey" >
    <tr><td colspan="2"><h2>@LangStrings.Login
            @if (Settings.AllowUserRegistration) {
                <span class="grey"><span style="font-weight:normal">@LangStrings.Or</span>
                <a href="@Url.Action("Register")">@LangStrings.Register</a></span>
            }
        </h2><div>@Html.ValidationSummary(false)</div></td></tr>
    <tr>
        <td><span style="font-size:14px">@LangStrings.Username @LangStrings.Or @LangStrings.Email</span></td>
        <td><span style="font-size:14px">@Html.TextBoxFor(m => m.Username, new { style = "width:100%" ,id="bjh",name="bjh"})</span></td>
    </tr>

    <tr>
        <td></td>
        <td style="padding-top: 16px;">
            <input id="Login" type="submit" value="@LangStrings.Login" style="font-weight:bold; font-size:16px " />
            @if (Settings.EnableSaml) { <text><input type="submit" name="samlButton" class="graybutton" value="SAML Login" /></text> }
        </td>
    </tr>
    <tr>
        <td></td>
        <td><a href="@Url.Action("LostPassword")" style="font-size:14px">@LangStrings.LostPassword</a>
        </td>
    </tr>
</table>
}

控制器

  [AllowAnonymous]
    public ActionResult LostPassword(string bjh)
    {
        var a =  bjh;
        if (string.IsNullOrWhiteSpace(a))
            {
                a = "empty";
            }

        return View(new LostPasswordModel() { Email = a });
    }

    [AllowAnonymous]
    [HttpPost]
    public ActionResult LostPassword(LostPasswordModel model)
    {
        if (model.Email.IndexOf("@") <0)
            {
                //not email, so clear found value

            }
        if (ModelState.IsValid)
        {
            if (!model.ResetPsw(Url.AbsoluteAction("Login", "User", null)))
                ModelState.AddModelError(String.Empty, LangStrings.Email_not_found);
            else
                ModelState.AddModelError(String.Empty, LangStrings.Success__An_email_has_been_sent__Check_your_inbox_after_a_while_);
        }
        return View(model);
    }

1 个答案:

答案 0 :(得分:0)

在视图中稍微改变href解决了问题,使用javascript和Jquery来获取值。

<a href="#" style="font-size:14px" onclick="window.location.href='@Url.Action("LostPassword")?bjh='+$('#bjh').val();">@LangStrings.LostPassword</a>