如何在使用jquery调用控制器函数时传递参数

时间:2010-10-26 09:41:26

标签: jquery asp.net-mvc-2 controller

我正在使用以下函数来调用控制器上的函数:

  function getPartialView(actionUrl, targetDiv, ajaxMessage, callback) {
        showAjaxMessage(targetDiv, ajaxMessage);

        $.get(actionUrl, null, function (result) {
            $(targetDiv).html(result);
            callback();
        });
    }

并将其称为:

  getPartialView("UCDamage", "#dvdamageAreaMid", "Loading...", function () { });

这个函数为我提供了类似于经典asp.net web表单中的updatePanel的功能。 现在请告诉我如何将函数中的值作为参数传递。

实际上UCDamage是一个用户控件,它将在div中运行:dvdamageAreaMid。 代码写在当前表单上,我在其上显示名为“UCDamage”的userControl。但我需要在控制器中将一些值传递给此函数。

我的控制器功能是这样的:

  public ActionResult UCDamage(string searchText)
    {           
        SecureModelService service = new SecureModelService();
        return View(service.ListBodyWork(searchText));
    }

我尝试过使用带有namex searchText的文本框并检索其值但无法获取它。

请给我一些建议并帮助我。 感谢

1 个答案:

答案 0 :(得分:1)

您可以通过$ .get调用中的第二个参数将值传递给控制器​​。

的内容
$.get(actionUrl, { searchText: "hello world" }, function (result) {
        $(targetDiv).html(result);
        callback();
    });

jQuery文档中有一些关于如何执行此操作的示例: http://api.jquery.com/jQuery.get/