简体   繁体   English

Url.Action调用Javascript函数

[英]Url.Action call Javascript Function

I have an Url Action Link that sends to my controller 1 paramater, but I need that paramater to call a javascript function to get document.getElementById and send that value to my controller. 我有一个Url Action Link,它向我的控制器发送了1个参数,但是我需要该参数来调用JavaScript函数来获取document.getElementById并将该值发送给我的控制器。 In my view I have the follwoing code: 在我看来,我有以下代码:

@foreach (var item in ViewBag.PersonsContacts as List<Software___FPPD.Models.Contact>)
{
    <tr>
        <td>@Html.DisplayFor(model => item.ContactType.Name)</td>
        <td>@Html.EditorFor(model => item.ContactValue, new { htmlAttributes = new { @class = "form-control", id = "contactValue"} })</td>
        <td>
            <a href="@Url.Action("EditPersonContact", "Person", new { contactValue = getValue(item.ContactValue)})" class="btn  btn-success">Alterar</a>
        </td>
    </tr>
}

My javascript: 我的JavaScript:

function getValue(contactValue) {
    document.getElementById("contactValue").value = contactValue; 
}

What I'm I doing wrong because I can not get this to work. 我做错了什么,因为我无法使它正常工作。

Url.Action is a method which gets executed in the server by razor and your javascript executes on the client side. Url.Action是一种由razor在服务器中执行的方法,而您的JavaScript在客户端执行。 So it is not that easy to mix both. 因此,将两者混合并不容易。

I am not sure what you are trying to do. 我不确定您要做什么。 In your question you mentioned you want to set value to some form field. 在您提到的问题中,您想将值设置为某个表单字段。 But since you are going to be redirected to the new page, there is no point ! 但是由于您将被重定向到新页面,所以没有意义! whatever value you set is gone (unless you are opening the new page in a new tab) 您设置的任何值都消失了(除非您在新标签页中打开新页面)

Anyway, What you can do is to listen for the click event of the anchor tag in your javascript and do whatever you want at the client side (ex: setting some form field value /executing a javascript function etc). 无论如何,您可以做的是在JavaScript中监听锚标记的click事件, click在客户端执行您想做的任何事情(例如:设置一些表单字段值/执行javascript函数等)。

You have some problem in your view code, you are creating the same id value for each item in the loop in @Html.EditorFor(model => item.ContactValue .Duplicate id's are not valid. So avoid that. 您的视图代码有问题,您正在@Html.EditorFor(model => item.ContactValue中为循环中的每个项目创建相同的ID值,重复的ID无效,因此请避免这种情况。

<td>
    @Html.EditorFor(model => item.ContactValue,
                     new { htmlAttributes = new { @class = "form-control myContactVal"} })
</td>
<td>
    <a href="@Url.Action("SignUp", "Home")"  data-contactvalue="@item.ContactValue" 
                                            class="btn  btn-success myEdit"> Alterar</a>
</td>

I added 2 new css classes , myContactVal and myEdit to the form fields to help us do our jQuery selection. 我在表单字段中添加了2个新的CSS类, myContactValmyEdit ,以帮助我们选择jQuery。 We are setting the item.ContactValue value in html5 data attribute to the anchor tag for accessing later in our javascript code. 我们正在将html5数据属性中的item.ContactValue值设置为锚标记,以便稍后在我们的JavaScript代码中进行访问。

And the javascript to handle the link click event 和JavaScript来处理链接点击事件

$(function () {

    $("a.myEdit").click(function (e) {

        e.preventDefault();
        _this = $(this);
        var contactVal = _this.data("contactvalue");
        var url = _this.attr("href");
        url = url + "?contactValue=" + contactVal;
        alert(url);

        //You can do any other thing here.

        //If you want to update the field with  myContactVal class in prev td
        _this.closest("tr").find(".myContactVal").val(contactVal);


       // finally do the redirect 
        window.location.href=url;

    })

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM