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

Natavia FinnieSenior Analyst Technical Focus ApplicationsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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

0
Natavia FinnieSenior Analyst Technical Focus ApplicationsAuthor Commented:
@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.
0
leakim971PluritechnicianCommented:
what do you get in the alert with :
//Javascript function
function GetRequestId(data) {
    var requestId = "";

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Natavia FinnieSenior Analyst Technical Focus ApplicationsAuthor Commented:
Awesome response time...
0
Natavia FinnieSenior Analyst Technical Focus ApplicationsAuthor Commented:
@leakim971,
I get the alert after it comes out the function. I truly appreciate your help but I taking another route.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.