locked
Not getting url last id RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    Currently my url as http://localhost:65099/Event/Create/93726

    After clicking a button I want to go to another actionresult with this id 93726

     @Html.ActionLink("Save as Draft", "SaveAsDraft", "Event", null, new { @class = "btn aqua block bottom15" })

    But I am unable to get this id. I am trying to access it as follows.

    int id = Convert.ToInt32(Request.Url.Segments.Last());
     public ActionResult SaveAsDraft(EventModel EM)
            {
               
              
    
                try
                {
    
                    int id = Convert.ToInt32(Request.Url.Segments.Last());
                 // doing something
    }             
                catch (Exception ex)
    {
      Web.MsgBox(ex.ToString());
    }

    As I put a breakpoing and it shows url as http://localhost:65099/Event/SaveAsDraft. That is why I am not getting the id.

    Please suggest. How to get the id and execute the SaveAsDraft actionresult with this id.

    Wednesday, August 21, 2019 1:33 PM

Answers

  • User-17257777 posted

    Hi demoninside9,

    Your ActionLink is incorrect, you should modify it like this.

    @Html.ActionLink("Save as Draft", "SaveAsDraft", "Event", new  { id = ViewBag.id }, new { @class = "btn aqua block bottom15"})

    Test with the following code:

    public ActionResult Edit(string id)
            {           
                RouteValueDictionary rvd = new RouteValueDictionary();
                rvd.Add("id", id);
                return RedirectToAction("Create", "Event", rvd);           
            }
    public ActionResult Create(int id) { //var id = rvd["id"]; ViewBag.id = id; return View(); }
    public ActionResult SaveAsDraft() { return View(); }

    Demo:

    Best Regards,

    Jiadong Meng.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2019 10:13 AM

All replies

  • User197322208 posted

    Your Id in the View should be in the Model of the View.( possibly EventModel ? ) And the Model is filled in the action

    So you will have 

    int id = Model.<ID property of the Model>;

    Wednesday, August 21, 2019 1:54 PM
  • User475983607 posted

    Simply add the route parameter.

     @Html.ActionLink("Save as Draft", "SaveAsDraft", "Event", new {id = id}, new { @class = "btn aqua block bottom15"})

    The code above assumes the name of the route parameter is "id" but you have not posted Create action or View so there is no way to provide accurate code.

    Wednesday, August 21, 2019 1:55 PM
  • User-17257777 posted

    Hi demoninside9,

    I suggest you could get the id in the “Create” Action and store it in ViewBag, then on the Create page, pass the Id to the Actionlink by ViewBag.

    @Html.ActionLink("Save as Draft", "SaveAsDraft", "Event", new { id = ViewBag.id }, new { @class = "btn aqua block bottom15"})


    Best Regards,
    Jiadong Meng.

    Thursday, August 22, 2019 3:38 AM
  • User1052662409 posted

    This is my edit method, when a row clicked for edit.

     public ActionResult Edit(string id, string Type, string Act)
            {
                if (Type == "Event" && Act == "Edit")
                {
                    objDB.Requesr_Master_For_Event_SelectById(Convert.ToInt32(id));
                    Session["Act"] = Act;
                    RouteValueDictionary rvd = new RouteValueDictionary();
                    rvd.Add("id", id);
                    return RedirectToAction("Create", "Event", rvd);
                }
                return View("Load");
            }

    Then it redirects to Create Actionresult with create view. There is a button on Create view 

     @Html.ActionLink("Save as Draft", "SaveAsDraft", "Event", null, new { @class = "btn aqua block bottom15",id="id" })

    I want when this button is clicked it should go the action result SaveAsDraft with the id.

    It goes but without id.

    Your Id in the View should be in the Model of the View.( possibly EventModel ? ) And the Model is filled in the action

    So you will have 

    int id = Model.<ID property of the Model>;

     through this method (objDB.Requesr_Master_For_Event_SelectById(Convert.ToInt32(id));) I am filling the EventModel but again I did not find the Model id with value.

    Thursday, August 22, 2019 6:19 AM
  • User-17257777 posted

    Hi demoninside9,

    Your ActionLink is incorrect, you should modify it like this.

    @Html.ActionLink("Save as Draft", "SaveAsDraft", "Event", new  { id = ViewBag.id }, new { @class = "btn aqua block bottom15"})

    Test with the following code:

    public ActionResult Edit(string id)
            {           
                RouteValueDictionary rvd = new RouteValueDictionary();
                rvd.Add("id", id);
                return RedirectToAction("Create", "Event", rvd);           
            }
    public ActionResult Create(int id) { //var id = rvd["id"]; ViewBag.id = id; return View(); }
    public ActionResult SaveAsDraft() { return View(); }

    Demo:

    Best Regards,

    Jiadong Meng.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2019 10:13 AM
  • User475983607 posted

    Sorry, I put the route parameter in the wrong place in my post.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.html.linkextensions.actionlink?view=aspnet-mvc-5.2

    Thursday, August 22, 2019 11:36 AM