locked
Calling an mvc controller using an Action Link from a Partial view RRS feed

  • Question

  • User2130491911 posted

    Hi all the question states is all

    I have this line of source code:

                    @Html.ActionLink("Request Password", "Get_Existing_Profiles", new { id = item.Email })

    The thought here is that I call the action result function passing in the user email. But I get a 404 error.

    The Action Result in the mvc controller

            [HttpGet]
            //Get Selected User Profiles 
            //ToDo: Add Try Catch
            public ActionResult Get_Existing_Profiles(string UserEmail)
            {
                if (UserEmail != null)
                {
    ...



    My other attempt was this:

                    @Html.ActionLink("Request Password", "RequestPassword", new { id = item.Email })

    This is in the controller

           [HttpPost]
            public ActionResult RequestPassword(string EmailAddress)
            {
                string UserPassword = Get_Exising_Users_Password(EmailAddress);
    
                return View(UserPassword);
            }

    What Appears to be happening is that I get the url with the email address in it.

    What I would like to have happen is that when the user clicks the link they program runs the function that gets the data from the database 

    and returns it to the screen or something that allows me to manipulate the returned information.

    Any idea what I am doing wrong here.. I modeled this after what I see is the wizards edit function: This works as expected..

      @Html.ActionLink("Edit", "Edit", new { id = item.AccRole_ID })

    Controller function

            public ActionResult Edit(int? id)
            {
    
                var model = new vw_xxxx_PROFILES();
    
    
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
    
                vw_xxx_PROFILES vw_xxx_PROFILES = db.vw_xxx_PROFILES.First(p => p.AccRole_ID == id);
                if (vw_xxx_PROFILES == null)
                    if (vw_xxx_PROFILES == null)
                    {
                        return HttpNotFound();
                    }
                return View(vw_xxx_PROFILES);
            }

    Monday, July 13, 2020 9:44 PM

Answers

  • User1686398519 posted

    Hi AppDev01,

    1. AppDev01

      But I get a 404 error.
      • Regarding this error, it is obvious that the path cannot be found. The following are the possible causes of this error after my test, please refer to it.
        1. If the controller where "Get_Existing_Profiles" is located and the controller where the partial view is located are not the same, then you need to specify the controller.
          • @Html.ActionLink("Request Password", "Get_Existing_Profiles", "Test16", new { UserEmail = item.Email }, null)
        2. If the above solution cannot solve the error, or they are the same controller, you can try to modify your code like this.For details, please refer to this link.
          • @Html.ActionLink("Request Password", "Get_Existing_Profiles",new { UserEmail = item.Email+"/" })
    2. AppDev01

      new { id = item.Email })

      AppDev01

      public ActionResult Get_Existing_Profiles(string UserEmail)
      • If you want to receive the parameter, then you need to have the same name, otherwise the accepted value is empty.You can change "id" to "UserEmail".

    Best regards,

    Yihui Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2020 8:23 AM
  • User1686398519 posted

    Hi AppDev01,

    Has your problem been solved? Are my suggestions helpful? If you have other needs, please tell us, we can give you help and suggestions.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 15, 2020 2:10 AM

All replies

  • User1686398519 posted

    Hi AppDev01,

    1. AppDev01

      But I get a 404 error.
      • Regarding this error, it is obvious that the path cannot be found. The following are the possible causes of this error after my test, please refer to it.
        1. If the controller where "Get_Existing_Profiles" is located and the controller where the partial view is located are not the same, then you need to specify the controller.
          • @Html.ActionLink("Request Password", "Get_Existing_Profiles", "Test16", new { UserEmail = item.Email }, null)
        2. If the above solution cannot solve the error, or they are the same controller, you can try to modify your code like this.For details, please refer to this link.
          • @Html.ActionLink("Request Password", "Get_Existing_Profiles",new { UserEmail = item.Email+"/" })
    2. AppDev01

      new { id = item.Email })

      AppDev01

      public ActionResult Get_Existing_Profiles(string UserEmail)
      • If you want to receive the parameter, then you need to have the same name, otherwise the accepted value is empty.You can change "id" to "UserEmail".

    Best regards,

    Yihui Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2020 8:23 AM
  • User2130491911 posted

    Thank you so much,  those mods worked and not when I click the actionlink when stepping through the program it calls the proper function in the controller but for some reason the ActionLink is not passing the email string value into the controller.

    This is the function call:

      @Html.ActionLink("Request Password", "RequestPassword", new { id = item.Email.ToString() + "/" } ,nu

    The call is going to the correct controller so I did not need to add the controller to the call.

    In the controller here is the function that I am calling I put it with the HttpGet... but I am not sure if it should be on the HttpGet of HttpPost as I am 100% clear on what they do really. I read up on them but I the information was a bit technical. 

            [HttpGet]
            public ActionResult RequestPassword(string EmailAddress)
            {
               
    
                string UserPassword = Get_Exising_Users_Password(EmailAddress);
    
                return View(UserPassword);
            }

    When I step into this function the Email Address is Null sadly

    I did some reading on how to pass the email as a parameters by editing the routing file so I have this entry

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

    Now when I run the program the URL does not show anything like it did before. Before adding this option I would at least see the email in the URL.

    Any one have any ideas? 

    Thanks?!
     

    I also attempted to mess with the routes but I am not sure I 

    Tuesday, July 14, 2020 8:33 PM
  • User2130491911 posted

    I figured this out.

    In the function call the name of the Parameter has to match the name of the parameter in the controller function. So that works!

              @Html.ActionLink("Request Password", "RequestPassword", new { EmailAddress = item.Email } ,null)

    Has to match the controller 

            [HttpGet]
            public ActionResult RequestPassword(string EmailAddress)
            {
               
    
                string UserPassword = Get_Exising_Users_Password(EmailAddress);
    
                return View(UserPassword);
            }

    I did change that http thing to HttpGet so maybe that help

    Tuesday, July 14, 2020 9:04 PM
  • User1686398519 posted

    Hi AppDev01,

    Has your problem been solved? Are my suggestions helpful? If you have other needs, please tell us, we can give you help and suggestions.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 15, 2020 2:10 AM
  • User2130491911 posted

    Yes I will and thank you I will mark your answer 

    Wednesday, July 15, 2020 2:01 PM