KMS
KMS

Reputation: 95

How can I pass email id to action method as parameter using @Html.ActionLink?

Index.chtml page itself i have this link

@Html.ActionLink("Edit","EditUserProfile",new {vchUserID = item.vchUserID})

Inside user Controller itself

// GET: /User/ViewUserProfile/1
public ActionResult EditUserProfile(string userID = "[email protected]")
{
    LIVE_USER objUserFind = db.LIVE_USER.Find(userID);

    if (objUserFind == null)
    {
        return HttpNotFound();
    }

    return View(objUserFind);

}

//
//POST: /Admin/EditAdminProfile/1
[HttpPost]
public ActionResult EditUserProfile(LIVE_USER objUserFind)
{
    if (ModelState.IsValid)
    {
        db.Entry(objUserFind).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(objUserFind);
}

RouteConfig.cs file itself i have url structure like bellow.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{vchUserID}",
            defaults: new { controller = "Admin", action = "Index", vchUserID = UrlParameter.Optional /*kareem removed vchUserID = UrlParameter.Optional*/         }
        );

Result i got is: *HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.*

Upvotes: 3

Views: 1701

Answers (1)

Daniel J.G.
Daniel J.G.

Reputation: 34992

The name of the parameter in your controller method needs to match the name of the parameter defined in your route. Currently the parameter is named vchUserID in the route but in your controller method you have named it userId.

That means the controller method will never get the value from the Url, and you will always get the default value "[email protected]". (I guess that is not one of your live users so you are returning the HttpNotFound result)

Try renaming the parameter in the controller method as in:

public ActionResult EditUserProfile(string vchUserID = "[email protected]")
{
    ...
}

Upvotes: 3

Related Questions