skip to Main Content

I want to show a warning that this year and season is available in the database when the repeated year and season is selected to enter information.But after showing this warning, because the page is refreshed, all the information entered in the inputs will be deleted.I want this information in the inputs not to be deleted and the user to choose another year and season

<form asp-controller="UserTax" asp-action="SabtKhareed" method="post">

    <div class="row d-flex justify-content-between align-items-center  p-2" style="background-color:#17a2b8; color:white">
        <h6>نام پروژه : <b class="text-warning">@ViewBag.proName</b></h6>

        <div class="d-flex">
            <label class="mt-2" style="margin-left:5px">سال</label>
            <select class="form-control" name="Yare" id="Yare" required  autocomplete="off">

                <option value="" default="" selected="">انتخاب کنید</option>

                <option value="1400">1400</option>
                <option value="1401">1401</option>
                <option value="1402">1402</option>
                <option value="1403">1403</option>
                <option value="1404">1404</option>
                <option value="1405">1405</option>
                <option value="1406">1406</option>
                <option value="1407">1407</option>
                <option value="1408">1408</option>
                <option value="1409">1409</option>
                <option value="1410">1410</option>

            </select>
        </div>

        <div class=" d-flex" style="margin-left:150px">
            <label label class="mt-2" style="margin-left:5px">فصل</label>
            <select class="form-control" name="FaslName" id="FaslName" required autocomplete="off">

                <option value="" default="" selected="">انتخاب کنید</option>

                <option value="بهار">بهار</option>
                <option value="تابستان">تابستان</option>
                <option value="پاییز">پاییز</option>
                <option value="زمستان">زمستان</option>
                <option value="سایر">سایر</option>

            </select>
        </div>


    </div>
    <hr />

    <div class="container-fluid" style="overflow: auto; overflow-x: hidden; direction:ltr ">

        <table class="table table-bordered table-striped table-hover  display" width="100%" style="direction:rtl;font-size:12px">
            <thead>

                <tr class="table-head-blue text-center" style="background-color: #416992; color: white; position: sticky; top: 0;">

                    <th>جزئیات</th>
                    <th>نام فروشنده</th>

         
                    <th style="font-size:12px">مبلغ خرید انجام شده</th>



                </tr>

            </thead>
            @{ var count = 0;}

            @for (var i = 0; i < Model.Count(); i++)

            {
                <tr>
                    <td class="text-center">
                        <a class="btn btn-secondary btn-sm " style="color:white; " data-toggle="modal" data-target="#myModal2@(count)"><i class="fa fa-info"></i></a>


                    </td>
                    <td class="text-center">@Model.ToList()[i].NameFroshande  @Model.ToList()[i].FamilyOrCoumpanyFroshande</td>


                    <td class="text-center">
                        <input class=" mt-1 text-sm text-center"   onkeyup="javascript:this.value=itpro(this.value);" name="[@i].MablaghKhreedAnjamShode" style="width:150px" required autocomplete="off" />

                    </td>

                  
                </tr>
                count++;
            }



        </table>
    </div>



    <div class=" text-center">
        <button class="btn btn-success  col-4 mb-2 " type="submit">ثبت اطلاعات</button>
    </div>

</form>

//——————————————————————————————

    public IActionResult SabtKhareedView(int id)
    {
        var pro = _context.Projects.Find(id);
        ViewBag.proName = pro.projectName;
        ViewBag.proID = pro.projectID;
        var list = _context.TaminKoonande.Where(a => a.ProjectID == id && a.VazyatTaminKoonande == 1).ToList();

        return View(list);
    }

    [HttpPost]
    public IActionResult SabtKhareed(IEnumerable<Khareed> khareed, int ProjectId, String Yare, String FaslName, int id)
    {
        var random = Convert.ToInt32(PublicClass.RandomClass.RandomString(6));
        var mainkareed = _context.MainKhareed.Where(a => a.ProjectId == ProjectId && a.Fasle == FaslName && a.Year == Yare).FirstOrDefault();

        if (mainkareed == null)
        {

            for (var i = 0; i < khareed.Count(); i++)
            {
                var taminName = _context.TaminKoonande.Find(khareed.ToList()[i].TaminKoonandeID);

                Khareed khareed1 = new Khareed();

                khareed1.MablaghKhreedAnjamShode = khareed.ToList()[i].MablaghKhreedAnjamShode;

                khareed1.ProjectId = khareed.ToList()[i].ProjectId;
                khareed1.Year = Yare;
         
                khareed1.Fasle = FaslName;
                khareed1.TaminKoonandeID = khareed.ToList()[i].TaminKoonandeID;
                khareed1.TaminKoonandeName = taminName.NameFroshande + " " + taminName.FamilyOrCoumpanyFroshande;
                khareed1.NoaeMooamele = "ریالی";

                _context.Khareed.Add(khareed1);
                _context.SaveChanges();

            }




            return RedirectToAction("KhareedListView", new { id });

        }

        TempData["Error"] = 1;

        return RedirectToAction("SabtKhareedView", new { id });
    }

enter image description here

2

Answers


  1. I recommend using Try-Catch. If you write your codes between Try-Catch, the page will not be reset unless the request is completed.

    try
    {
        code here...
    }
    catch (Exception ex)
    {
    
    Login or Signup to reply.
  2. Keep the table content existed, then per my knowledge, using ajax here would be a better solution.

    Firstly, removing the asp-controller="Home" asp-action="subData" method="post" and type="submit" in the form element. Then we can add id for tbody and button. Adding click event handler and modify the controller api:

    @section Scripts{
        <script>
            $("#Yare").val(@ViewBag.option1);
            $("#FaslName").val(@ViewBag.option2);
            $("#btn1").click(function(){
                alert(1);
                $.ajax({
                    url: "https://localhost:7175/Home/subData",
                    type:"post",
                    data:{
                        "Yare": $("#Yare").val(),
                        "FaslName": $("#FaslName").val()
                    },
                    success:function(data){
                        alert(data);
                        $("#content1").empty();
                        $("#content1").append("<tr><td>hello</td><td>world</td></tr>")
                    }
                });
            });
        </script>
    }
    
    //[HttpPost]
    //public IActionResult subData(String Yare, String FaslName) {
    //    return RedirectToAction("Privacy", new { Yare, FaslName });
    //}
    
    [HttpPost]
    public string subData(String Yare, String FaslName)
    {
          return "error";
    }
    

    ============================================

    In my humble opinion, what you want is that, users select an option in the selector, then submit the searching, and after returning the response, the selector can keep the option still selector. So here’s my sample. When go back to the view, bring the selected option value and using js code to set the selected option.

    <form asp-controller="Home" asp-action="subData" method="post">
        <div class="d-flex">
            <label class="mt-2" style="margin-left:5px">سال</label>
            <select class="form-control" name="Yare" id="Yare" required autocomplete="off">
                <option value="" default="" selected="">انتخاب کنید</option>
                <option value="1400">1400</option>
                <option value="1401">1401</option>
                <option value="1402">1402</option>
                <option value="1403">1403</option>
                <option value="1404">1404</option>
            </select>
        </div>
        <div class=" d-flex" style="margin-left:150px">
            <label label class="mt-2" style="margin-left:5px">فصل</label>
            <select class="form-control" name="FaslName" id="FaslName" required autocomplete="off">
                <option value="" default="" selected="">انتخاب کنید</option>
                <option value="1">option1</option>
                <option value="2">option2</option>
                <option value="3">option3</option>
            </select>
        </div>
        <div class=" text-center">
            <button class="btn btn-success  col-4 mb-2 " type="submit">ثبت اطلاعات</button>
        </div>
        <div>option1 : @ViewBag.option1</div>
        <div>option2 : @ViewBag.option2</div>
    </form>
    
    @section Scripts{
        <script>
            $("#Yare").val(@ViewBag.option1);
            $("#FaslName").val(@ViewBag.option2);
        </script>
    }
    
    public IActionResult Privacy(String Yare, String FaslName)
    {
        ViewBag.option1 = Yare;
        ViewBag.option2 = FaslName;
        return View();
    }
    
    [HttpPost]
    public IActionResult subData(String Yare, String FaslName) {
        return RedirectToAction("Privacy", new { Yare, FaslName });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search