将动态值传递给ASP.NET MVC 5中的Url.Action

时间:2015-08-01 15:44:55

标签: asp.net asp.net-mvc-5

这是我观点的一部分:

<a href="@Url.Action("ForgotPassword","Account",new{id="temp"})" id=" lnk"> ForgotMyPassword</a>

这是我在论坛中找到的JQuery代码,用于动态地将文本框的值传递给Url.Action:

<script type="text/javascript">
    $("#lnk").click(function (evt) {
        var temporary= $("#lnk").prop("href");
        var uri = temporary.replace("temp", $("#Username").val());
    });
</script>

情景是,如果用户不记得密码,他会输入用户名并点击链接并进入密码恢复页面。我的问题是它发送的值总是临时值而不是文本框值。如何修复它,如果有更好的方法,我会很高兴听到它。

更新:这是我的整个表格:

@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { @class = "form-signin", role = "form" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    @Html.Label("username")
                    @Html.TextBoxFor(m => m.Username, new { @class = "form-control", placeholder = "username" })
                    @Html.ValidationMessageFor(m => m.Username, "", new { @class = "text-danger", style = "display:block;margin-top:3px;" })
                    @Html.Label("pass")
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "password" })
                    @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })

                    <a href="@Url.Action("ForgotPassword","Account",new{id="temp"})" id=" lnk">forgot my password</a>
                    <input type="submit" value="login" class="btn btn-primary btn-block" />
                }

1 个答案:

答案 0 :(得分:0)

好的,这是使用partialviews和AJAX调用的可能解决方案 如您所见,表单内部有一个部分视图。每次在Username输入中键入任何内容时,它都会触发仅执行AJAX调用的事件,并传递该值并替换部分视图。

解决方案1(部分浏览)

您的表单

@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { @class = "form-signin", role = "form" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.Label("username")
    @Html.TextBoxFor(m => m.Username, new { @class = "form-control", placeholder = "username" })
    @Html.ValidationMessageFor(m => m.Username, "", new { @class = "text-danger", style = "display:block;margin-top:3px;" })
    @Html.Label("pass")
    @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "password" })
    @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })

    <div id="_linkPartial">
        @Html.Partial("_linkPartial", String.Empty);
    </div>
    <input type="submit" value="login" class="btn btn-primary btn-block" />
}

<script>
    $(document).ready(function () {
        $('[id^="Username"]').keyup(function () {
            debugger;
            var url = '/Account/RefreshLink';
            var username = this.value;
            $.ajax({
                type: 'GET',
                url: url,
                data: { value: username },
                success: function (result) {
                    $('#_linkPartial').html(result);
                },
                error: function (result) {
                    // handle errors
                    location.href = "/Account/"
                }
            });
        });
    });
</script>

<强> _linkPartial

<a href="@Url.Action("ForgotPassword","Account",new{id=Model})" id=" lnk">forgot my password</a>

帐户控制器

public ActionResult ForgotPassword(string id)
{
    //whatever you want to do...

    return View();
}

public ActionResult RefreshLink(string value)
{
    return PartialView("_linkPartial", value);
}

解决方案2(更换HREF)

您的表单

@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { @class = "form-signin", role = "form" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.Label("username")
    @Html.TextBoxFor(m => m.Username, new { @class = "form-control", placeholder = "username" })
    @Html.ValidationMessageFor(m => m.Username, "", new { @class = "text-danger", style = "display:block;margin-top:3px;" })
    @Html.Label("pass")
    @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "password" })
    @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })

    <a href="@Url.Action("ForgotPassword","Account",new{id=""})" id="lnk">forgot my password</a>
    <input type="submit" value="login" class="btn btn-primary btn-block" />
}

<script>
    $(document).ready(function () {
        $('[id^="Username"]').keyup(function () {
            debugger;
            var username = this.value;
            var linkcontrol = $("#lnk");
            var newUrl = '@Url.Action("ForgotPassword", "Account", new { id = "_id_" })'.replace('_id_', username);
            linkcontrol.attr("href", newUrl)
        });
    });
</script>

无论如何,我认为其他具有登录/忘记密码形式的解决方案可能会更好。