Retain DropDownList selected value after submitting Form in ASP.Net Core MVC

sangyongjin88
 
on Jun 17, 2022 11:00 PM
Sample_888647.zip
401 Views

Hi,

I have problem to get data using DropDownList.

After I selected date from DropDownList, then submit button, that DropDownList reset to 0 value.

I want to keep data even though I clicked submit button not to reset value which is "0".

Please check my code and help me how to hold data?

<h2 id="Choosedurrent">Choose Current</h2>
    <select id="YearCu" name="YearCu" style="width: 150px;height: 40px;">
        <option value="0">Please Select Year</option>
        <option value="2021">2021</option>
        <option value="2022">2022</option>
        <option value="Other">Other</option>
    </select>
    <br />
    <select id="Month" name="Month" style="width: 150px;height: 40px;" onchange="this.form.submit()" >
        <option value="0">Please Select Month</option>
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">March</option>
        <option value="4">April</option>
        <option value="5">May</option>
        <option value="6">June</option>
        <option value="7">July</option>
        <option value="8">Aug</option>
        <option value="9">Sep</option>
        <option value="10">Oct</option>
        <option value="11">Nov</option>
        <option value="12">Dec</option>
    </select>
</td>
<td style="padding:20px;">
    <h2 id="Comparess">Compare </h2>
    <select id="YearCom" name="YearCom" style="width: 150px;height: 40px;">
        <option value="0">Please Select Year</option>
        <option value="2021">2021</option>
        <option value="2022">2022</option>
 
    </select>
    <br />
    <select id="MonthCom" name="MonthCom" style="width: 150px;height: 40px;">
        <option value="0">Please Select Month</option>
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">March</option>
        <option value="4">April</option>
        <option value="5">May</option>
        <option value="6">June</option>
        <option value="7">July</option>
        <option value="8">Aug</option>
        <option value="9">Sep</option>
        <option value="10">Oct</option>
        <option value="11">Nov</option>
        <option value="12">Dec</option>
    </select> 
    <select id="txtOther01" name="Presetss" style="width: 220px; height: 40px; display: none;">
        <option value="0">Please Select Preset</option>
        <option value="ytd">Year-To-Date</option>
 
    </select>
</td> 
[HttpGet]
public ActionResult SalesGraph(string YearCu, string Month, string YearCom, string MonthCom, string Presetss)
{
    var Username5 = User.Identity.Name;
    ViewBag.Arrival = Month;
 
    var ok1 = db.Users.Where(x => x.UserName == Username5).FirstOrDefault();
    if (ok1 != null)
    {
        ViewBag.Ok2 = ok1.Po_Name;
        ViewBag.Ok3 = ok1.Hired_date.ToString("MM/dd/yyyy");
        ViewBag.Ok4 = ok1.Department2;
        ViewBag.Ok5 = ok1.User_Point;
        ViewBag.Ok6 = ok1.Name;
        ViewBag.Ok9 = ok1.Pto_Point;
        ViewBag.Ok10 = ok1.Personal_Leave;
        ViewBag.Ity = db.Messages.Where(x => x.Isread == false && x.MessageTo == ok1.Name).Count();
 
        if (ok1.Position_order <= 5)
        {
            ViewBag.Ok7 = "Manager";
        }
        if (ok1.Position_order == 3 && ok1.Department1 == "Retail Experience")
        {
            ViewBag.Ok8 = "Manager";
        }
    return View();
}
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 17, 2022 11:20 PM

Hi sangyongjin88,

Do you want to retain all the DropDownList selected value?

dharmendr
 
on Jun 18, 2022 07:18 AM

Hi sangyongjin88,

In order to retain the selected value you need to Set the value in TempData and retrieve the TempData in View page to set the DropDownList value using JavaScript.

Refer below example.

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Index(string year, string month)
    {
        TempData["Year"] = year;
        TempData["Month"] = month;
        return View();
    }
}

View

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Index">
        <select id="ddlYears" name="Year" style="width: 150px;height: 40px;">
            <option value="0">Please Select Year</option>
            <option value="2021">2021</option>
            <option value="2022">2022</option>
            <option value="Other">Other</option>
        </select>
        <select id="ddlMonths" name="Month" style="width: 150px;height: 40px;" onchange="this.form.submit()">
            <option value="0">Please Select Month</option>
            <option value="1">Jan</option>
            <option value="2">Feb</option>
            <option value="3">March</option>
            <option value="4">April</option>
            <option value="5">May</option>
            <option value="6">June</option>
            <option value="7">July</option>
            <option value="8">Aug</option>
            <option value="9">Sep</option>
            <option value="10">Oct</option>
            <option value="11">Nov</option>
            <option value="12">Dec</option>
        </select>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        @if (TempData["Year"] != null)
        {
            <script type="text/javascript">
                $(function () {
                    $("#ddlYears").val(@TempData["Year"]);
                });
            </script>
        }

        @if (TempData["Month"] != null)
        {
            <script type="text/javascript">
                $(function () {
                    $("#ddlMonths").val(@TempData["Month"]);
                });
            </script>
        }
    </form>
</body>
</html>

Screenshot