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

Natavia Finnie
Natavia Finnie used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

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

Natavia FinnieSenior Analyst Technical Focus Applications

Author

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.
Multitechnician
Top Expert 2014
Commented:
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

Natavia FinnieSenior Analyst Technical Focus Applications

Author

Commented:
Awesome response time...
Natavia FinnieSenior Analyst Technical Focus Applications

Author

Commented:
@leakim971,
I get the alert after it comes out the function. I truly appreciate your help but I taking another route.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial