Wednesday, 26 September 2012

MVC - C# - Simple Example how to pass parameters

The following simple example will show how to create simple controller with function that accepts some parameters:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCTesting.Models;

namespace MVCTesting.Controllers
{
    public class UsersController : Controller
    {
        //
        // GET: /Users/

        UserTable usr = new UserTable();

        public ActionResult Index()
        {
            var users = from user in usr.Users
                        select user;

            return View(users);
        }

        public string TestingParameters(string name, int age = 30)
        {
            return "hello " + name + " your age is " + age.ToString();
        }

    }
}

go to
http://localhost:52421/Users/TestingParameters?name=John&age=40
and the following message will be displayed
hello John your age is 40

go to
http://localhost:52421/Users/TestingParameters?name=John
and the following message will be displayed
hello John your age is 30
30 is default parameter

No comments:

Post a Comment