如何调用从jQuery的一个ActionResult [英] how to call An ActionResult from Jquery

查看:155
本文介绍了如何调用从jQuery的一个ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ActionResult

this is my ActionResult

{          

    UsersModel um = new UsersModel();
    um.Users = userRepository.GetAllUsers();
    um.UserCustomers = userRepository.GetAllUserCustomerConnections();
    um.UserTypes = enums.GetAllDescriptions(CodeType.UserType);
    um.Customers = userRepository.GetAllCustomers();
    um = SearchUsers(Request,um);
    return View(um);
}

它采用了功能SearchUsers:

it uses the function SearchUsers :

    private UsersModel SearchUsers(HttpRequestBase request, UsersModel curModel)
    {
        try
        {
            // request parameters
            string userName = request.Params["user-name"];
            string firstName = request.Params["first-name"];
            string lastName = request.Params["last-name"];
            int status,type,businessId;

            if (!string.IsNullOrWhiteSpace(userName))
                curModel.Users = curModel.Users.Where(u => u.Username.Contains(userName));
            if (!string.IsNullOrWhiteSpace(firstName))
                curModel.Users = curModel.Users.Where(u => u.FirstName.Contains(firstName));
            if (!string.IsNullOrWhiteSpace(lastName))
                curModel.Users = curModel.Users.Where(u => u.LastName.Contains(lastName));
            if (int.TryParse(request.Params["status-search"], out status))
                curModel.Users = curModel.Users.Where(u => u.Status == status);
            if (int.TryParse(request.Params["userTypes-search"], out type))
                curModel.Users = curModel.Users.Where(u => u.UserType == type);
            if (int.TryParse(request.Params["busi-name"], out businessId))
                curModel.Users = curModel.Users.Where(u => u.LastCustomerId == businessId);

            return curModel;
        }
        catch
        {
            return curModel;
        }

    }

现在我有我的看法与ID按钮搜索用户
和我的js文件的命令:

now i have on my view a button with the id "search-users" and my command in the js file :

$('#search-users').click(function () { 

    });

我怎么可以张贴的Htt prequestBase到控制器?

how can i post the HttpRequestBase to the controller ?

推荐答案

我不能完全回答这个问题,因为我不知道一切究竟是如何做的结构。

I can't answer this fully because I don't know exactly how everything is structured.

无论调用您的操作使用AJAX或者不是你仍然会有请求期间相同的对象。这意味着,你仍然有一个请求对象的类型为的Htt prequestBase 。这是一个好消息;这意味着处理Ajax请求比较简单。

Whether the call to your action is using AJAX or not you will still have the same objects during the request. This means that you will still have a Request object that is of type HttpRequestBase. This is good news; it means that handling AJAX requests is relatively simple.

首先,决定如何处理你的动作,这样它适合于它与AJAX使用。您可以使用 Request.IsAjaxRequest()分支你的逻辑。

Firstly, decide how to handle your action so that it's appropriate for it to be used with AJAX. You can use Request.IsAjaxRequest() to branch your logic.

例如:

{          
    UsersModel um = new UsersModel();
    um.Users = userRepository.GetAllUsers();
    um.UserCustomers = userRepository.GetAllUserCustomerConnections();
    um.UserTypes = enums.GetAllDescriptions(CodeType.UserType);
    um.Customers = userRepository.GetAllCustomers();
    um = SearchUsers(Request,um);
    if (Request.IsAjaxRequest())
    {
        return PartialView(um);
    }
    return View(um);
}

在你看来,你希望你的按钮

In your view you want your button to


  1. 发送使用AJAX请求

  2. 请与响应的东西。

要做到这一点可能是您的按钮以发送一个请求到操作和为动作返回单程一 PartialView (如在上面的例子)。这意味着你将需要与返回的HTML来替换页的内容。

One way to do this might be for your button to send a request to the action and for the action to return a PartialView (as in the example above). This means that you will need to replace the contents of the page with the returned HTML.

您按钮看起来像一个良好的开端。

Your button looks like a good start.

您将需要你的AJAX脚本解释请求的URL,有些数据要发送,哪些与反应(至少)。示例(非功能性的,这只是一个指南的):

You'll need your AJAX script to explain which URL to request, some data to send and what to with the response (at a minimum). Example (non-functional, this is only a guide):

$('#search-users').click(function () { 
    $.ajax({
        url: "@Url.Action("Index", "MyController")",
        data: {user-name: $("#user-name").val(),
               first-name: $("#first-name").val(),
               last-name: $("#last-name").val()},
        success: function(data) {
                $("#content").html(data);
            };
        }
    );
});

这篇关于如何调用从jQuery的一个ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆