Natavia Finnie
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)
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();" })
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 };
}
//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);
}
//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;
};
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome response time...
ASKER
@leakim971,
I get the alert after it comes out the function. I truly appreciate your help but I taking another route.
I get the alert after it comes out the function. I truly appreciate your help but I taking another route.
The issue is your line 17 (location.href = '/Request/Create/' ) is executed just after(<1ms) line 5
so do :
Open in new window