Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp mvc http get action with object as parameter

In my controller I got action:

[HttpGet]
public ActionResult CreateAdmin(object routeValues = null)
{
    //some code
    return View();
}

And http post:

[HttpPost]
 public ActionResult CreateAdmin(
    string email, 
    string nameF, 
    string nameL, 
    string nameM)
 {
        if (email == "" || nameF == "" || nameL == "" || nameM == "")
        {
            return RedirectToAction("CreateAdmin", new
            {
                error = true,
                email = email,
                nameF = nameF,
                nameL = nameL,
                nameM = nameM,
            });
  }

variable routeValues in http get Action is always empty. How correctly pass object as parameter to [http get] Action?

like image 270
Yuriy Mayorov Avatar asked Jul 09 '13 20:07

Yuriy Mayorov


1 Answers

You cannot pass an object to GET, instead try passing individual values like this:

[HttpGet]
public ActionResult CreateAdmin(int value1, string value2, string value3)
{
    //some code
    var obj = new MyObject {property1 = value1; propety2 = value2; property3 = value3};
    return View();
}

You can then pass values from anywhere of your app like:

http://someurl.com/CreateAdmin?valu1=1&value2="hello"&value3="world"
like image 114
aliirz Avatar answered Nov 15 '22 21:11

aliirz