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

时间:2022-11-30 21:08:40

This is a part of my view:

这是我观点的一部分:

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

and here is my JQuery code that I found in a forum for dynamically passing the value of text box to the Url.Action:

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

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

The Scenario is if user doesn't remember the password, he types his username and clicks on the link and goes to password recovery page. My problem is that the value it sends is always temp and not the textbox value. How can I fix it and if there is any better approach, I'll be glad to hear it.

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

Update:This is my entire form:

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

@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 个解决方案

#1


0  

Ok, here is a possible solution using the partialviews and an AJAX call As you can see, there is a partialview inside your form. Everytime you type anything in the Username input it fires the event that just executes an AJAX call passing the value and replaces the partialview.

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

SOLUTION 1 (partialview)

解决方案1(部分视图)

Your form

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

Account Controller

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

    return View();
}

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

SOLUTION 2 (Replacing HREF)

解决方案2(更换HREF)

Your form

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

Anyway, I think other solutions with isolated forms for login/forgotpassword might be better.

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

#1


0  

Ok, here is a possible solution using the partialviews and an AJAX call As you can see, there is a partialview inside your form. Everytime you type anything in the Username input it fires the event that just executes an AJAX call passing the value and replaces the partialview.

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

SOLUTION 1 (partialview)

解决方案1(部分视图)

Your form

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

Account Controller

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

    return View();
}

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

SOLUTION 2 (Replacing HREF)

解决方案2(更换HREF)

Your form

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

Anyway, I think other solutions with isolated forms for login/forgotpassword might be better.

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