Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to pass string value(UserId) from one controller to http post action result(button clik)

What I have tried:

C#
public ActionResult Login(string UserId)
    {
       // string mail = Decrypt(UserId);
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(UserProfile objUser)
    {

        //string EncryptedID = Decrypt(objUser.UserId);
        if (ModelState.IsValid)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = db.UserProfiles.Where(a => a.UserName.Equals(objUser.UserName) && a.Password.Equals(objUser.Password)).FirstOrDefault();
                if (obj != null)
                {
                    Session["UserId"] = obj.UserId.ToString();
                    Session["UserName"] = obj.UserName.ToString();
                    return RedirectToAction("UserDashBoard");
                }
            }
Posted
Updated 26-May-17 2:40am
v2
Comments
Richard Deeming 26-May-17 11:14am    
And why are you re-inventing the wheel? ASP.NET has several perfectly good authentication / authorization systems built-in.

For example, ASP.NET Identity[^]

1 solution

You can create a anchor tag in combination with @Url.Action method, so when the user will click it, it will call the Login action and will post the UserId with value IdHere for now:

HTML
<a href="@Url.Action("Login","ControllerName", new {UserId = "IdHere"})"


or you can use the button element as well like:

HTML
<button class="btn btn-info" type="button" id="addressSearch"   
          onclick="location.href='@Url.Action("Login","ControllerName", new {UserId = "IdHere"})'">



Hope it helps you to understand how to approach this.

Thanks,
Ehsan
 
Share this answer
 
v2
Comments
GrpSMK 26-May-17 10:47am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900