admin管理员组

文章数量:1336304

I have java script function

   function SetTextBoxValue(data) {
        $('#text_box').val($(this).val("myField"))

    };

Is it possible, and how, to call only this function, WITHOUT controller and action names from @Html.ActionLink() in razor view?

To extend a question, i have table and a form with 4 text boxes on same view. I need to fill one textbox with a value of a specific column in table when user click on link in that column.

I have java script function

   function SetTextBoxValue(data) {
        $('#text_box').val($(this).val("myField"))

    };

Is it possible, and how, to call only this function, WITHOUT controller and action names from @Html.ActionLink() in razor view?

To extend a question, i have table and a form with 4 text boxes on same view. I need to fill one textbox with a value of a specific column in table when user click on link in that column.

Share asked Oct 27, 2014 at 1:35 onedevteam.onedevteam. 4,17810 gold badges44 silver badges75 bronze badges 3
  • The script does not make sense (what is $(this) in the context of the function?). Show the html and indicate what your trying to do. – user3559349 Commented Oct 27, 2014 at 1:44
  • $(this) is @html.actionlink defined as " @Html.ActionLink(modelItem.myfield, "controller", "action" new { OnClick = "SetTextBoxValue()" })" – onedevteam. Commented Oct 27, 2014 at 1:48
  • 1 Why use Html.Actionlink if you only need js code to execute? – David Tansey Commented Oct 27, 2014 at 2:19
Add a ment  | 

2 Answers 2

Reset to default 2

try this :

@Html.ActionLink("LinkText", "#", "", null, new { onclick="SetTextBoxValue('mydata')" });

Hope this helps.

You need 2 steps :

1 : add onclick="return someJsFct" in your actionLink

@Html.ActionLink("click here", "", "", null, new { onclick="return SetTextBoxValue('data')" });

2 : your javascript function must return false to prevent the actionLink from calling the server

<script language="javascript">
    function SetTextBoxValue(data) {
        // some code

        return false;
    }
</script>

本文标签: aspnet mvcHow to call ONLY javascript function in htmlactionlinkStack Overflow