Hi @Jerry Lipan ,
Set drop down value to New York (default value), then
To set the default select value, you can use the SelectListItem.Selected Property, for example, add the Selected property to the "New York" option.
public IActionResult Index()
{
//Creating the List of SelectListItem, this list you can bind from the database.
List<SelectListItem> cities = new()
{
new SelectListItem { Value = "1", Text = "New York", Selected = true },
new SelectListItem { Value = "2", Text = "Milan" },
new SelectListItem { Value = "3", Text = "Tokyo" },
new SelectListItem { Value = "4", Text = "Seoul" },
};
//insert the first option
cities.Insert(0, new SelectListItem() { Value = "0", Text = "-- Select City --" });
//assigning SelectListItem to view Bag
ViewBag.cities = cities;
return View();
}
Partial View ( InfoBox01.cshtml ) automatically display the result
Then, in the view page, after the document is ready, you can use JQuery to get the Selected value, if the selected value is not the first option ("0"), load the partial view to display the related content.
<script type="text/javascript">
$(document).ready(function(){
$("#products").change(function () {
$("#InfoBox").load("@Url.Action("InfoBox01")" + "?value=" + this.value);
});
var selectvalue = $("#products").val();
if(selectvalue!="0")
$("#InfoBox").load("@Url.Action("InfoBox01")" + "?value=" + selectvalue);
});
</script>
The result is like this:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion