I have inserted a Bootstrap DateTimePicker in my Index Razor Page. If I select a date, I call my c# method "OnGetMyFunctionAsync()".
I use Ajax to call the method and it start well, but I don't know how to get the value of the selected date.
Maybe I don't understand how Razor pages works.
This is my .cs file:
//[BindProperty]
public string strMyDate { get; set; }
public async Task OnGetMyFunctionAsync()
{
try
{
// Get the Selected Date
//
//strMyDate = Request.Form["strMyDate"];
strMyDate = Request.Query["strMyDate"];
[ .. code .. ]
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
I have tried to use the [BindProperty] to define mu "strMyDate" variable. And I have tried to get the value of the string using "Request.Form" and "Request.Query", but nothing.
This is my Index.cshtml page. Note that using "@Model.strMyDate" the value is regularly showed in the input box.
public async Task<IActionResult> OnGetMyFunctionAsync()
{
try
{
var str = Request.Query["strMyDate"];
return new JsonResult(str);
}
catch (Exception ex)
{
var errorMessage = ex.Message;
return new JsonResult(errorMessage);
}
}
Result:
Best Regards,
Jerry Cai
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
thank you very much for your explanation. Now It works. Thank you for the high details of your answer.
I don't know where is my post! This is a real mystery! I'll repost it again, as a new answer. I don't know if some administrator could help me to adjust the things.
I have inserted a Bootstrap DateTimePicker in my Index Razor Page. If I select a date, I call my c# method "OnGetMyFunctionAsync()".
I use Ajax to call the method and it start well, but I don't know how to get the value of the selected date.
Maybe I don't understand how Razor pages works.
This is my .cs file:
//[BindProperty]
public string strMyDate { get; set; }
public async Task OnGetMyFunctionAsync()
{
try
{
// Get the Selected Date
//
//strMyDate = Request.Form["strMyDate"];
strMyDate = Request.Query["strMyDate"];
[ .. code .. ]
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
I have tried to use the [BindProperty] to define mu "strMyDate" variable. And I have tried to get the value of the string using "Request.Form" and "Request.Query", but nothing.
This is my Index.cshtml page. Note that using "@Model.strMyDate" the value is regularly showed in the input box.
Member
38 Points
217 Posts
Get the value of the Date in Bootstrap DateTimePicker in razor page
Feb 18, 2021 03:57 PM|igorbaldacci|LINK
Hi,
I have inserted a Bootstrap DateTimePicker in my Index Razor Page. If I select a date, I call my c# method "OnGetMyFunctionAsync()".
I use Ajax to call the method and it start well, but I don't know how to get the value of the selected date.
Maybe I don't understand how Razor pages works.
This is my .cs file:
I have tried to use the [BindProperty] to define mu "strMyDate" variable. And I have tried to get the value of the string using "Request.Form" and "Request.Query", but nothing.
This is my Index.cshtml page. Note that using "@Model.strMyDate" the value is regularly showed in the input box.
Please, could someone help me to understand where I'm wrong.
Thanks.
Igor.
All-Star
58124 Points
15641 Posts
Re: Get the value of the Date in Bootstrap DateTimePicker in razor page
Feb 18, 2021 04:35 PM|bruce (sqlwork.com)|LINK
several issues with your code. the handler should return an action result
the property must be bound for get:
the jquery does not pass any values.
$.ajax({ type: 'GET', data: {strMyDate : e.date}, url: '/Index?handler=MyFunction', dataType: 'json' });
Member
38 Points
217 Posts
Re: Get the value of the Date in Bootstrap DateTimePicker in razor page
Feb 18, 2021 06:03 PM|igorbaldacci|LINK
Hi bruce,
I'm trying your suggestione. Could you please specify me the "JsonResult(...)?
I have tried the follow but doesn't work, I receive the null value of "strMyDate":
And in the Ajax call, If I use "e.date" I receive an error, so I have tried this:
Thank you.
Participant
940 Points
313 Posts
Re: Get the value of the Date in Bootstrap DateTimePicker in razor page
Feb 19, 2021 06:34 AM|Jerry Cai|LINK
Hi,igorbaldacci
Where is the original post, could you please share your razor view?
I can get the data in this demo, you can check this:
Result:
Best Regards,
Jerry Cai
Member
38 Points
217 Posts
Re: Get the value of the Date in Bootstrap DateTimePicker in razor page
Feb 19, 2021 07:52 AM|igorbaldacci|LINK
Hi Jerry,
thank you very much for your explanation. Now It works. Thank you for the high details of your answer.
I don't know where is my post! This is a real mystery! I'll repost it again, as a new answer. I don't know if some administrator could help me to adjust the things.
igorbaldacci
Member
38 Points
217 Posts
Re: Get the value of the Date in Bootstrap DateTimePicker in razor page
Feb 19, 2021 07:53 AM|igorbaldacci|LINK
Hi,
I have inserted a Bootstrap DateTimePicker in my Index Razor Page. If I select a date, I call my c# method "OnGetMyFunctionAsync()".
I use Ajax to call the method and it start well, but I don't know how to get the value of the selected date.
Maybe I don't understand how Razor pages works.
This is my .cs file:
I have tried to use the [BindProperty] to define mu "strMyDate" variable. And I have tried to get the value of the string using "Request.Form" and "Request.Query", but nothing.
This is my Index.cshtml page. Note that using "@Model.strMyDate" the value is regularly showed in the input box.
Please, could someone help me to understand where I'm wrong.
Thanks.
Igor.
Member
38 Points
217 Posts
Re: Get the value of the Date in Bootstrap DateTimePicker in razor page
Feb 19, 2021 10:55 AM|igorbaldacci|LINK
Hi Jerry, just a little questions: how to refresh the page to reload the new selected data?