简体   繁体   中英

Problem with passing parameter and recieving paramenter in javascript Function

I am using a Action link button my project and using a Javascript function as well. which is updating my 2 partial view but when I run my project It gives me an error.

The name 'temp' does not exist in the current context

              <%= Ajax.ActionLink("Select", "Employee", new { Id = Employee.EmployeeID }, newAjaxOptions { UpdateTargetId = "Employee",HttpMethod="Post", OnSuccess = "EmployeeHistory('" + Employee.EmployeeID.ToString() + "')" })%>

and let the javascript function I m calling is as

     <script language="javascript" type="text/javascript">
      function EmployeeHistory(EmployeeID) {
       $('#EmployeeInformation').load('<%= Url.Action("EmployeeInformation", "Home",new { Id=EmployeeID}) %>');
       }

       function success(result){
       $("#EmployeeInformation").html(result);

You cannot mix server side code with javascript like this ( Url.Action runs on the server while EmployeeID is a javascript variable):

$('#EmployeeInformation').load('<%= Url.Action("EmployeeInformation", "Home",new { Id=EmployeeID}) %>');

Try the following:

var url = '<%= Url.Action("EmployeeInformation", "Home") %>';
$('#EmployeeInformation').load(url, { id: EmployeeID }));

Also you need to define the OnSuccess like this:

OnSuccess = "function() { EmployeeHistory('" + Employee.EmployeeID.ToString() + "'); }"

But quite quite honestly I would use jQuery with a standard Html.ActionLink helper and do things unobtrusively:

<%= Html.ActionLink(
    "Select", 
    "Employee", 
    new RouteValueDictionary(new { id = Employee.EmployeeID }),
    new Dictionary<string, object> { 
        { "id", "selectEmployee" }, 
        { "data-id", Employee.EmployeeID } 
    }
) %>

and then I would unobtrusively AJAXify it:

<script type="text/javascript">
    $(function() {
        $('#selectEmployee').click(function() {
            var link = $(this);
            $.post(this.href, function(result) {
                $('#Employee').html(result);
                var url = '<%= Url.Action("EmployeeInformation", "Home") %>';
                $('#EmployeeInformation').load(url, { id: link.data('id') });
            });
            return false;
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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