whoah
whoah

Reputation: 4443

get value from parameter MVC

How can I get a value from parameter? I have links:
1) http://localhost:2409/Account/Confirmation/16 or
2) ../Account/Confirmation/12ds-2saa-fcse
I want to get "16" or "12ds-2saa-fcse" in controler method.

my method

    public ActionResult Confirmation(int id)
    {
        ViewBag.Message = "ID is: " + id;
        return View();
    }

but it returns null.

Routes:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

How can I do this?

EDIT!

Everything works perfercly, I don't know why, but restart VS2012 helped.. :O

Now is another problem. Is there any possibility to get hash value from this link? /Account/Confirmation/16?hash=dasdsadasda. Code below doesn't show hash string..

    public ActionResult Confirmation(int id, string hash)
    {
        ViewBag.Message = "ID is: " + id + HttpUtility.HtmlEncode("hash");
        return View();
    }

Upvotes: 0

Views: 476

Answers (1)

Ant P
Ant P

Reputation: 25221

Your updated problem is that you are encoding the string "hash"; not the value of the String variable hash.

This:

public ActionResult Confirmation(int id, string hash)
{
    ViewBag.Message = "ID is: " + id + HttpUtility.HtmlEncode("hash");
    return View();
}

Should become this:

public ActionResult Confirmation(int id, string hash)
{
    ViewBag.Message = "ID is: " + id + HttpUtility.HtmlEncode(hash);
    return View();
}

Upvotes: 1

Related Questions