I'm working on a single page web app. The page has a dropdown list. When an item is selected, I use jQuery to post the selected value to an action method, which [for now, for testing purposes] adds that value to the ViewBag, and returns a PartialView. I want to put this partial view in the same page, obviously. So when I select a value from the dropdown, the selected option should show up below it. Is this possible or am I approaching this the wrong way?
Relevant code for context:
Index (Main page)
<html>
@using SampleTracking.Models.ViewModels;
@model SamplingEventsVM
<head>
<title>@ViewBag.Title</title>
<script src="~/Scripts/jquery-2.1.1.js"></script>
<script type="text/javascript" src="~/Scripts/CustomScripts.js"></script>
</head>
<body>
<span id="SamplingEventDiv">
@Html.DropDownListFor(model => model.SelectedSamplingEvent, Model.SamplingEvents, new { @id = "SamplingEventSelection" })
</span>
<div id="SampleListDiv">
@{Html.RenderPartial("~/Views/Home/RetrieveSamples.cshtml");}
</div>
</body>
</html>
Script
$(function (ready) {
$("#SamplingEventSelection").change(
function () {
$.post(
"/Home/RetrieveSamples",
{ selectedSamplingEvent: $("#SamplingEventSelection").val() },
function (data) {
$("#SamplingEventDetails").html(data)
}
)
}
)
});
Action method script is posting to
public ActionResult RetrieveSamples(string samplingEvent)
{
ViewBag.Selected = samplingEvent;
return PartialView();
}
Partial view
<div id="SamplingEventDetails" style="margin-top:100px;">@ViewBag.Selected</div>