I have a drop down list. If the user selects a click on a Fetch call, I want to pass the identifier of the selected Call to the FetchCall action in the Person controller.
<p> @Html.DropDownListFor(m => m.CallsToMake, new SelectList(Model.CallsToMake.OrderByDescending(call => call.TimeStamp), null), new { @class = "btn btn-default dropdown-toggle", style = "width: 500px;" }) </p> <p> @Html.ActionLink("Fetch call","FetchCall", "Person", new { id = HERE_SELECTED_CALL_ID }, new { @class = "btn btn-info btn-lg" }) </p>
The view is strongly typed: @model WebApplication2.Models.ApplicationUser and is associated with Action Details in the ApplicationUsers controller.
How to get the value selected in @ Html.DropDownList for inside the view and pass it to the action in ANOTHER controller in my case?
If this is not a problem, I feel better with a java script (I better understand what is happening).
EDIT
This is a solution (mine) that lacks the value from the dropdown in the java script function.
Drop-down list:
@Html.DropDownListFor(m => m.CallsToMake, new SelectList(Model.CallsToMake.OrderByDescending(call => call.TimeStamp), null), new { @class = "btn btn-default dropdown-toggle", style = "width: 500px;" })
The button is called when javascript:FetchCall() pressed javascript:FetchCall() :
<input type="button" id="FetchCallButton" value="Fetch call" onclick="javascript:FetchCall()" />
JavaScript function:
function FetchCall() { var url = '@Url.Action("FetchCall, "Person")'; $.post(url, { callToMakeId: SELECTED_CALL_IN_DROPDOWN_ID_HOW_TO_GET_IT}, function (data) { alert('YES'); }); }
Action method in PersonController :
public ActionResult FetchCall(int callToMakeId) {...}
Question: How to get selected call.Id in java script function?
EDIT 2
Thanks to Tommy's solution, I was able to read the selection in the drop-down list. The problem is that I get what is written in the drop-down list, which is the result of the ToString() class inside CallToMake .
function:
function FetchCall() { alert($("#CallsToMake").val()); var url = '@Url.Action("FetchCall", "Person")'; $.post(url, { callToMakeId: $("#CallsToMake").val() }, function (data) { alert('YES'); }); }
The image below shows the contents of the drop-down list and alert alerts alert($("#CallsToMake").val()); :

The only solution that comes to my mind is to display the call id in the drop-down list and then cut it in the controller action. That sounds bad, right?
EDIT 3
As a result, the list is as follows:
@Html.DropDownListFor(m => m.CallsToMake, new SelectList(Model.CallsToMake.OrderByDescending(call => call.TimeStamp), "Id","Id"), new { @class = "btn btn-default dropdown-toggle", style = "width: 500px;" })
java script:
function FetchCall() { alert($("#CallsToMake").val()); var url = '@Url.Action("FetchCall", "Person")'; $.post(url, { callToMakeId: $("#CallsToMake").val() }, function (data) { alert('YES'); }); } =
and method of action
public ActionResult FetchCall(int callToMakeId) { CallToMake callToMake = db.CallsToMake.Find(callToMakeId); int idPersonToCall = callToMake.Callee.Id; db.CallsToMake.Remove(callToMake); db.SaveChanges(); Debug.WriteLine("I AM HERE"); return RedirectToAction("Details", new { id = idPersonToCall }); }
The action method is called, but I'm not redirected anywhere. The method just starts, and thatβs it.