Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Url.Action in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 401
    Comment on it

    While writing projects in MVC we need to sometimes send our URL to the action where the methods will be called.

     

    So here we are about to discuss a property called Url.Action which is used to redirect and navigate the view to the particular action.

     

    <table id="grid" data-source="@Url.Action("GetEmployees")"></table>

     

    Here we have write this method to navigate to the action where we want it to go in the controller action.

     

     [HttpGet]       
            public JsonResult GetEmployees(int? page, int? limit, string sortBy, string direction, string searchString = null)
            {
                int total;
                var records = new GridModel().GetEmployeeList(page, limit, sortBy, direction, searchString, out total);
                return Json(new { records, total }, JsonRequestBehavior.AllowGet);
            }

     

    So we have made a call to the method GetEmployeeList through the action named GetEmployees.

     

       public List<EmployeeModel> GetEmployeeList(int? page, int? limit, string sortBy, string direction, string searchString, out int total)
            {
                total = (from u in DataContextObj.EmployeeDetails select u).Count();
                var records = (from p in DataContextObj.EmployeeDetails
                               select new EmployeeModel
                               {
                                   EmpId = p.EmpId,
                                   FirstName = p.EmpFirstName,
                                   MiddleName = p.EmpMiddleName,
                                   LastName = p.EmpLastName
                               }).AsQueryable();
    
                if (!string.IsNullOrWhiteSpace(searchString))
                {
                    records = records.Where(p => p.FirstName.Contains(searchString) || p.LastName.Contains(searchString));
                }
    
                if (!string.IsNullOrEmpty(sortBy) && !string.IsNullOrEmpty(direction))
                {
                    if (direction.Trim().ToLower() == "asc")
                    {
                        records = SortHelper.OrderBy(records, sortBy);
                    }
                    else
                    {
                        records = SortHelper.OrderByDescending(records, sortBy);
                    }
                }
    
                if (page.HasValue && limit.HasValue)
                {
                    int start = (page.Value - 1) * limit.Value;
                    records = records.Skip(start).Take(limit.Value);
                }
    
                return records.ToList();
            }

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: