Link to home
Start Free TrialLog in
Avatar of Natavia Finnie
Natavia FinnieFlag for United States of America

asked on

Passing a parameter from an ActionLink call to pass to the Create action controller but need the Id from a hidden field

I am trying to pass an actionlink parameter to the "Create" action in the controller. I am trying to create and insert a row into the database because I need the "RequestID" for later use.  What I have tried thus far is to create a prop in the my viewmodel named "NewRequestId".  I am calling an ajax "GET" json request to send the requestId to a hidden field on the "Index" view.  The main object here is to have the requestId at the time the "public ActionResult Create()", which is the HttpGet, gets called. The hidden textbox field is being populated but it does not seem to want to pass to the controller.

1. Index
2. GetRequest to
the 3. Create (But I need to have to the requestId from the database at this time)

//this is on the Index.chtml
@Html.ActionLink("Create New", "Create", "Request", null, new { id = NewRequestId, onclick = "GetRequestId();" })

Open in new window


public JsonResult GetRequestId()
{
            Request request = new Request();
            DateTime now = DateTime.Now;

            User userid = requestRepository.GetUserByUserName(Environment.UserName);            

            request.UserName = userid.UserName;
            request.RequestDate = now;
            request.TicketStatusId = 7; // 7 = Created, 1 = Submitted
            request.HasNotifications = false;
            request.AddToWatchList = false;

            db.Requests.Add(request);

            db.SaveChanges();
            
            return new JsonResult { Data = request, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

Open in new window


//GET: Request/Create
public ActionResult Create(int requestId)
{
Request request = db.Requests.Find(requestId);

//Add the rest of the data to the request record and update that id

db.SaveChanges();

return View(request);
 }

Open in new window


//Javascript function
function GetRequestId(data) {
    var requestId = "";

    $.ajax(
        {
            url: '/Request/GetRequestId',
            //data: { requestId },
            type: "GET",
            dataType: "json",
            success: function (result) {
                $("#NewRequestId").val(result.RequestId);
                requestId = result.RequestId;
            }            
        });
   
    location.href = '/Request/Create/' + $("#NewRequestId").val();
    return false;
};

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Ajax call is asynchronous by default. Image it took one hour...ok 1 minutes... ok ok... 5 seconds to save the data and return the RequestId

The issue is your line 17 (location.href = '/Request/Create/' ) is executed just after(<1ms) line 5
so do :
//Javascript function
function GetRequestId(data) {
    var requestId = "";

    $.ajax(
        {
            url: '/Request/GetRequestId',
            //data: { requestId },
            type: "GET",
            dataType: "json",
            success: function (result) {
                location.href = '/Request/Create/' + result.RequestId;
            }            
        });
 
        return false;
}; 

Open in new window

Avatar of Natavia Finnie

ASKER

@leakim971, I tried this way too. However, just to make sure I tried it again. It still passes a null value to the '/Request/Create/' + result.RequestId.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome response time...
@leakim971,
I get the alert after it comes out the function. I truly appreciate your help but I taking another route.