locked
problem : search in db base date RRS feed

  • Question

  • User1928394251 posted

    hello . i have paersiancalendar in my form . i select date , i post  selected value  from view to controller with data picker .  datapicker get persiandate , put in  another textbox for example .it is right now .

    but i have problem :

    discription : (my date saved in db geogorian date) .i select persian , get my datapicker persian date .),(in my model datetime date )

    i write in my controller :

    public void showvalue1(string filter)

    {
     var data = filter;
     int year = Convert.ToInt32(data.Substring(0, 4));
     int month = Convert.ToInt32(data.Substring(5, 2));
     int day = Convert.ToInt32(data.Substring(8, 2));
     DateTime dt= new DateTime(year, month, day, new System.Globalization.PersianCalendar());
     return dt;
    }

    public ActionResult search31(DateTime dt)
    {

     var entity = from m in db.Notes select m;

    if (dt!= null)
     {

     entity = entity.Where(m => m.date == dt);

    }

    else {

    return RedirectToAction("search");

    }
    return View(entity);

    }

    but it dont work . please help me .

    Tuesday, June 18, 2019 12:23 PM

Answers

  • User1928394251 posted

    hello . thank you for your answer . i found my problem . my form in view was right . my datepicker was right . but i cant use from binder in my project . it solves to my friend .

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 3:22 AM
  • User1520731567 posted

    Hi aabedeni056,

     I'm glad that you have solved the issue by yourself.

    I suggest you could mark the right reply as answer.

    This will help other people who faces the same issue.

    Thank you very much.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 9:28 AM
  • User1928394251 posted

    <script type="text/javascript">

    $(document).on('focus', 'input.datepicker', function () {
    $(this).datepicker({
    changeMonth: true, //
    changeYear: true, // T
    yearRange: 'c-100:c+0'
    });
    });
    </script>

    </head>

    <body>
    <div>
    @Html.ActionLink("برگشت به صفحه اصلی", "Index")
    </div>

    <div class="form-group">

    <div class="col-md-10">

    @using (Html.BeginForm("search34", "entities", new { }, FormMethod.Post, new { @class = "form-horizontal", autocomplete = "Off" }))

    {

    @Html.EditorFor(model => model.date)


    <button type="submit" class="btn-default"> جستجو بصورت ماهانه </button>


    }

    </div>
    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 11:15 AM

All replies

  • User1520731567 posted

    Hi aabedeni056,

    Do you mean you want to filter data based on selected datetime,but page can't show the response data?

    Firstly,check if your return of action is correct,

    you could modify code like:

    public ActionResult search31(DateTime dt)
    {
    if (dt!= null) {
     var entity = (from m in db.Notes  
                  where m.date == dt
    select m).ToList(); } else { return RedirectToAction("search"); } return View(entity); //add breakponits on it and check if entity is right }

    And then.if entity is right,you need to open F12 developer tool to check if the html in Razor receive the entity correctly.

    If you still have problems,please post more details.

    Best Regards.

    Yuki Tao

    Wednesday, June 19, 2019 9:29 AM
  • User1928394251 posted

    hello.thanks your answer . yes . i maen it .

    Wednesday, June 19, 2019 9:48 AM
  • User1928394251 posted

    i use datepicker , persian calendar in my form search view .

    my view :

    <script type="text/javascript">

    $(function showvalue1()

    {
    $(".datepicker").datepicker();
    $(".datepicker").datepicker({ dateFormat: 'yyyy/mm/dd', altFormat: 'yyyy/mm/dd'});

    var dateTime = $('.datepicker').datepicker('getDate');
    var dateTime = new Date($(".datepicker").datepicker("getDate"));
    var strDateTime = dateTime.getFullYear() + "/" + (dateTime.getMonth() + 1) + "/" + dateTime.getDate();
    var filter = { date: strDateTime };

    $.ajax
    ({
    type: "post",
    dataType: "json",
    data: filter

    });
    url: "@Url.Action("search32", "entities")";
    });
    </script>

    <body>

    <div class="form-group">

    <div class="col-md-10">

    @{ Html.BeginForm("search32", "entities", FormMethod.Get); }

    تاریخ را انتخاب نمایید @Html.TextBoxFor(model => model.date, new { @id = "txtDate",onclick = "PersianDatePicker.Show(this, '1398/03/13')" ,@class= "datepicker" })

    <input type='submit' value='جستجو بر اساس تاریخ' name="date" />
    @{ Html.EndForm(); }
    </div>
    </div>
    <script src="~/Scripts/PersianDatePicker.js"></script>

    </body>

    my controller :

    public int[] GetDateParts(string filter)

    {

    return filter.Split('/').Select(datePart => int.Parse(datePart)).ToArray();

    }


    public DateTime showvalue1(string filter)

    {

    PersianCalendar pc = new PersianCalendar();

    var dateParts = GetDateParts(filter);
    //var dateParts = GetDateParts(st);
    DateTime dt = pc.ToDateTime(dateParts[0],dateParts[1],dateParts[2], 0, 0, 0, 0, 0);

    return dt;

    }

    public ActionResult search32(DateTime dt)
    {

    var entity = from m in db.Notes select m;


    if (dt != null)
    {

    entity = entity.Where(m => m.date == dt );

    return view(entity);

    }
    else
    {

    return RedirectToAction("search");

    }

    return View(entity); //add breakponits on it and check if entity is right

    }

    }

    Wednesday, June 19, 2019 9:55 AM
  • User1928394251 posted

    before i use from your code .i recive this error :

    The parameters dictionary contains a null entry for parameter 'dt' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult search32(System.DateTime)' in 'MainNoteIt.Controllers.entitiesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
    Parameter name: parameters

    Wednesday, June 19, 2019 9:56 AM
  • User1520731567 posted

    Hi aabedeni056,

    aabedeni056

    The parameters dictionary contains a null entry for parameter 'dt' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult search32(System.DateTime)' in 'MainNoteIt.Controllers.entitiesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

    According to your error message,your parameters type is wrong,and I saw your ajax,the parameters name is not right.

    I suggest you could modify ajax and search32 action,like:

         $.ajax
                ({
                type: "post",
                dataType: "json",
                data: filter,
                url: "@Url.Action("search32", "entities")",
                        success: function (data) {
    // <input type='text' class="selectdate" /> $(".selectdate").val(data.xxx); //put in another textbox:if you need to show fetch data in textbox
    //if fetch data is a record with some fields,you could data.xxx1,data.xx2
    //if fetch data is a single record,you could use data directly } });
            [HttpPost]
            public JsonResult search32(string dt) 
            {
    
                ....
    
               var entity = (from m in db.Notes  
    where m.date == dt
    select m).ToList(); ... return Json(entity, JsonRequestBehavior.AllowGet); }

    Finally,in my project,I can show the fetch data in textbox successfully.

    Best Regards.

    Yuki Tao

    Thursday, June 20, 2019 8:44 AM
  • User1928394251 posted

    hello . thank you for your answer . i found my problem . my form in view was right . my datepicker was right . but i cant use from binder in my project . it solves to my friend .

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 3:22 AM
  • User1520731567 posted

    Hi aabedeni056,

     I'm glad that you have solved the issue by yourself.

    I suggest you could mark the right reply as answer.

    This will help other people who faces the same issue.

    Thank you very much.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 9:28 AM
  • User1928394251 posted

    hello.thank you . now ,i don know how search in db base "month" in persian calendar .

    Tuesday, June 25, 2019 11:06 AM
  • User1928394251 posted

    <script type="text/javascript">

    $(document).on('focus', 'input.datepicker', function () {
    $(this).datepicker({
    changeMonth: true, //
    changeYear: true, // T
    yearRange: 'c-100:c+0'
    });
    });
    </script>

    </head>

    <body>
    <div>
    @Html.ActionLink("برگشت به صفحه اصلی", "Index")
    </div>

    <div class="form-group">

    <div class="col-md-10">

    @using (Html.BeginForm("search34", "entities", new { }, FormMethod.Post, new { @class = "form-horizontal", autocomplete = "Off" }))

    {

    @Html.EditorFor(model => model.date)


    <button type="submit" class="btn-default"> جستجو بصورت ماهانه </button>


    }

    </div>
    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 11:15 AM