最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass parameter from MVC view to an action with jQuery - Stack Overflow

programmeradmin0浏览0评论

I want to pass a parameter from a view to an action through the following jQuery code, but I don't succeed to pass the parameter. Am I missing something?

$('li').click(function () {  
employeeID = $(this).attr('id');    
window.location.href = '/ApproveWork/GetEmployeeDays/'+employeeID; 
});

Controller:

[HttpGet]
public ActionResult GetEmployeeDays(string userCode)
{
     .....
    return View("GetEmployeeDays", model);
}

routing from Global.asax

public static void RegisterRoutes(RouteCollection routes)
 {
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
           new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );          
  }

P.S. I tried to use AJAX jQuery function, but this doesn't return me the view that I specified in the Action.

I want to pass a parameter from a view to an action through the following jQuery code, but I don't succeed to pass the parameter. Am I missing something?

$('li').click(function () {  
employeeID = $(this).attr('id');    
window.location.href = '/ApproveWork/GetEmployeeDays/'+employeeID; 
});

Controller:

[HttpGet]
public ActionResult GetEmployeeDays(string userCode)
{
     .....
    return View("GetEmployeeDays", model);
}

routing from Global.asax

public static void RegisterRoutes(RouteCollection routes)
 {
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
           new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );          
  }

P.S. I tried to use AJAX jQuery function, but this doesn't return me the view that I specified in the Action.

Share Improve this question edited Jan 18, 2013 at 13:22 pisi asked Jan 18, 2013 at 11:34 pisipisi 1271 gold badge4 silver badges16 bronze badges 3
  • 1 Where is parameter_to_pass defined and what is it's value? Also, seeing your action method may help. – Rory McCrossan Commented Jan 18, 2013 at 11:36
  • 2 @pisi Can you post your controller code? – Hary Commented Jan 18, 2013 at 11:38
  • 1 @pisi Can you post your routing code? – Steve's a D Commented Jan 18, 2013 at 13:15
Add a comment  | 

4 Answers 4

Reset to default 9

Is your javascript on the view page

If so use the Url.Action helper to build the url in the normal way

var url = '@Url.Action("Action", "Controller", new {parametername = "REPLACEME"})';
window.location.href = url.Replace('REPLACEME', parameter_value);

Updated to account for client side variable *Update 2: updated typo*

Did you try with ajax?

$.ajax({
  url: '@Url.Action("GetEmployeeDays", "ApproveWork")',
  type: 'GET',
  dataType: 'html',
  contentType: 'application/json; charset=utf-8',
  data: { userCode: $(this).attr('id') }
 })

Can you please check your Global.asax file the register routes?

Please check the optional variable name over there.

Suppose you have register route using following.

routes.MapRoute( _
            "Default", _
            "{controller}/{action}/{id}", _
            New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
        )

You have to write your action below way.

[HttpGet]
public ActionResult GetEmployeeDays(int id)
{
     .....
    return View("GetEmployeeDays", model);
}

or

[HttpGet]
public ActionResult GetEmployeeDays(string id)
{
     .....
    return View("GetEmployeeDays", model);
}

You cannot pass parameter with the help of @Url.Action with jquery. You can create initial string containing url and then you can pass that string to window.location.href

$_BaseUrl="Your application url";

<script>
 Window.location.href = $_BaseUrl + '/Admin/Account/SwitchSuperAdminLogin?UserName=' + user + '&Password=' + pass + '&IndustryId=' + indus;
 </script>
发布评论

评论列表(0)

  1. 暂无评论