locked
Persian datepicker doesn't keep the previous chosen date after clicking submit button RRS feed

  • Question

  • User-997500692 posted

    I'm implementing asp.net core 3.1. I'm using a persian datepicker and for implementing it I'm using below links: https://github.com/Mds92/MD.BootstrapPersianDateTimePicker

    https://forums.asp.net/post/6272109.aspx

    My code to implement it is like the following:

    public class MainViewModel
        {
            public string StartDate { get; set; }
            public string EndDate { get; set; }
    }


    Here is my code in Index view:

                <form method="post" style="height: 4rem;">
        
                <div class="col-sm-9 col-12 text-left row  align-items-center">
        
                    <div class="border-light col-3 input-group" style="margin-right: 3.5rem !important;">
                        <div class="input-group-append text-self-center form-control center p-2 pl-2 bg-light col-4">
                            <span class="oi oi-calendar pl-2"></span><span>from </span>
                        </div>
                        <input type="text" id="StartDate" name="StartDate" class="form-control datepicker" placeholder="@ViewBag.StartDate" />
                    </div>
        
                    <div class="border-light col-3 input-group">
                        <div class="input-group-append text-self-center form-control center p-2 pl-2 bg-light col-4 ">
                            <span class="oi oi-calendar pl-2"></span><span>till </span>
                        </div>
                        <input type="text" id="EndDate" name="EndDate" class="form-control datepicker" placeholder="@ViewBag.EndDate" />
                    </div>
                  
                <div class="border-light col-2 input-group">
                <input type="submit" value="submit" asp-action="Index" class="btn btn-outline-dark p-0 px-2" />
        
                    </div>
                </div>
         </form>
        <script>
              $(function () {
                        $(".datepicker").each(function () {
                            var kk = '#' + $(this)[0].id;
                            $(this).MdPersianDateTimePicker({
                                targetTextSelector: kk,
                                dateFormat: 'yyyy-MM-dd',
                                isGregorian: false,
                              enableTimePicker: false
                                
                            });
                        });
                    });
        </script>


    And here is my Index method in Controller:

       public IActionResult Index(MainViewModel m)
        {
           
    
            string startDate = String.IsNullOrEmpty(m.StartDate) ? PersianDateTime.Now.Date.ToString(format: "yyyy/MM/dd").PersianToEnglish() : FaToEn(m.StartDate);
    
            string endDate = String.IsNullOrEmpty(m.EndDate) ? PersianDateTime.Now.Date.ToString(format: "yyyy/MM/dd").PersianToEnglish() : FaToEn(m.EndDate);
            string regionID = Request.Form["RegionList"].ToString();
    
            return (BuildIndexModel(startDate, endDate, regionID));
        }


    When I choose my considering date from the datepickers, my project runs perfect, however, if I choose a date from one of datepickers while not selecting any date from the other one, the other one doesn't keep the previous chosen date and its date changes to todays date. I appreciate if any one tells me how can I keep always the previous dates on the date picker if I don't change the datepickers dates after clicking on submit button.



    Sunday, September 27, 2020 5:15 AM

All replies

  • User1312693872 posted

    Hi,minamrm

    Do you mean you can't pass the previous value in viewbag to action? You can change placeholder to value :

    <div class="border-light col-3 input-group" style="margin-right: 3.5rem !important;">
                <div class="input-group-append text-self-center form-control center p-2 pl-2 bg-light col-4">
                    <span class="oi oi-calendar pl-2"></span><span>from</span>
                </div>
                <input type="text" id="StartDate" name="StartDate" class="form-control datepicker" value="@ViewBag.StartDate" />
            </div>
    
            <div class="border-light col-3 input-group">
                <div class="input-group-append text-self-center form-control center p-2 pl-2 bg-light col-4 ">
                    <span class="oi oi-calendar pl-2"></span><span>till</span>
                </div>
                <input type="text" id="EndDate" name="EndDate" class="form-control datepicker" value="@ViewBag.EndDate" />
            </div>

    Result:

    Best Regards,

    Jerry Cai

    Monday, September 28, 2020 2:48 AM