简体   繁体   English

Asp.net内核刷新页面后如何设置输入值

[英]how to Set the entered values after refreshing the page in Asp.net core

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 });
    }

在此处输入图像描述

I recommend using Try-Catch.我推荐使用 Try-Catch。 If you write your codes between Try-Catch, the page will not be reset unless the request is completed.如果您在 Try-Catch 之间编写代码,除非请求完成,否则不会重置页面。

try
{
    code here...
}
catch (Exception ex)
{

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.以我的拙见,您想要的是,用户 select 在选择器中选择一个选项,然后提交搜索,返回响应后,选择器可以保持该选项仍然选择器。 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.当 go 回到视图时,带上选中的选项值并使用 js 代码设置选中的选项。

<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 });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 JQuery 和 ASP.NET CORE 中发布 function 后表格未刷新 - Table not refreshing after post function in JQuery and ASP.NET CORE 在asp.net中重新加载页面后如何保留JavaScript值? - How to preserve JavaScript values after page reload in asp.net? 如何在不刷新ASP.NET页的情况下更新数据库 - How to update database without refreshing the page ASP.NET 关闭 asp.net 内核中的模态窗体后如何刷新父页面 - How to refresh the Parent page after you close the modal form in asp.net core 登录 Asp.net 核心 mvc 后如何重定向到上次访问的页面 - How to Redirect to Last Visited page After Login in Asp.net core mvc 刷新页面后 localStorage 值设置为 undefined - localStorage values are set to undefined after refreshing the page ASP.NET-刷新页面后不使用viewstate和autopostback保留下拉列表的选定值的状态 - ASP.NET - Keeping the state of the selected value of the dropdown after refreshing the page without using viewstate and autopostback 从用户控件刷新asp.net页面到页面顶部 - Refreshing an asp.net page to top of page from user control 回发后的页面上的asp.net jquery加载值 - asp.net jquery loading values on page after post back 更改后如何在asp.net Core中选择呼叫控制器 - How call controller after change select in asp.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM