【问题标题】:Second Form Action, how to get value from Html.TextBoxFor第二种形式的动作,如何从 Html.TextBoxFor 中获取价值
【发布时间】:2016-05-26 20:55:57
【问题描述】:

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

我想将用户名传递到密码丢失屏幕。我添加了 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);
    }

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多