View :
<select id="count" name="count" class="form-control">
<option value="0">Choose</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
</select>
Jquery:
<script>
$("#count").change(function () {
let count = $("#count :selected").val();
if (count != 0) {
$.getJSON("/AdminPanel/Poll/Create/" + count);
}
});
</script>
Controller :
public IActionResult Create(int id)
{
ViewBag.Count = id;
return View();
}
How can Send Value As Viewbag to View From Controller?
I want use in loop... in razor page
You can't. What I'd recommend is having a separate action method:
public IActionResult Create(int id)
{
return Json(new { count = id });
}
And then in the GET request:
$("#count").change(function () {
let count = $("#count :selected").val();
if (count != 0) {
$.getJSON("/AdminPanel/Poll/Create/" + count, function(d) {
$("#count").val(d.count);
});
}
});
It's essentially syncing the two results by updating the server and refreshing the UI on the client. ViewBag is not recognized in the JavaScript callback, but you can return whatever you want and then make the update in JS. You can return a PartialView to replace part of the HTML page, for instance.