Cannot display Boostrap-Datepicker in asp.net-mvc Razor pages

SSRS-VS 46 Reputation points
2021-06-11T06:26:41.097+00:00

I'm trying to implement an input datepicker in my asp.net-mvc project that display only years to create/edit new values of my model. I want the day and the month to be always the same (31/12/year) and allow the user to select only the year. Something like this:

104539-imagen.png

For that, I'm using the code from here: https://qawithexperts.com/article/bootstrap/select-only-year-in-bootstrap-datepicker/323

At the begginig, it didn't work:

104598-imagen.png

But lattely I found why. In the _layout.cshtml I have the default view when you create an asp.net-mvc project. And If I delete this: @Scripts.Render("~/bundles/jquery") It will work:

104540-imagen.png

But it is not correct to delete that since I need it for other things in other views. So my question is how to use the "only-year boostrap-datepicker" with @Scripts.Render("~/bundles/jquery") and why is this error happening?

Thank you.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,156 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,251 questions
0 comments No comments
{count} votes

Accepted answer
  1. Chao Deng-MSFT 796 Reputation points
    2021-06-11T08:16:44.627+00:00

    Hi @SSRS-VS ,
    There is no need to delete @Scripts.Render("~/bundles/jquery"), you can do so.
    If you do not need to use the layout view for the current page, you can set it to null.

    @{  
        ViewBag.Title = "Index";  
        Layout = null;  
      
    }  
    

    Result:
    dyysP.png

    If you need to use the current layout view, you can do this.

    Fgqnn.png

    Result:
    6zAHq.png


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    ChaoDeng

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. SSRS-VS 46 Reputation points
    2021-06-11T08:25:12.76+00:00

    Thank you so much @Chao Deng-MSFT Now it works.

    Could you explain why that error happen before? Why do you need to put inside @section script hat one and not the other two? It is because some conflict withy @Scripts.Render("~/bundles/jquery")?


  2. SSRS-VS 46 Reputation points
    2021-06-11T09:03:48.87+00:00

    Thanks @Chao Deng-MSFT
    My second question is how set the day and month from the script.

    This is the new form for my model:
    104708-imagen.png

    And I want fix the day and month when I select the year:

    104734-imagen.png

    How can I set model=>Date.value = "31/12/" + selected year in the create and edit view so I can pass the value to the controller and pass de modelSatatet.isValid???

    Date field in my model:

    104761-imagen.png