简体   繁体   English

使用JavaScript / jQuery将Bootstrap Datepicker中的值传递给C#Datetimeoffset可空类型

[英]Passing Value from Bootstrap Datepicker to C# Datetimeoffset Nullable type using javascript / jQuery

I have a Bootstrap datepicker in my View. 我的视图中有一个Bootstrap日期选择器 I have set one of my model variable to public DateTimeOffset? expiryDate { get; set; } 我已将我的模型变量之一设置为public DateTimeOffset? expiryDate { get; set; } public DateTimeOffset? expiryDate { get; set; } public DateTimeOffset? expiryDate { get; set; } The Expiry date is an Optional field .So it's the User who uses the application decides whether to set an expiry date or not during a process. public DateTimeOffset? expiryDate { get; set; }到期日期是一个可选字段。因此,由用户使用应用程序来决定是否在流程中设置到期日期。 by using $('#datepickerid').val() i can get the current value from Datepicker and i need to send it to the controller via an ajax call. 通过使用$('#datepickerid').val()我可以从Datepicker获取当前值,我需要通过ajax调用将其发送到控制器。 I use var expirydate = $("#datepickerid").val(); 我使用var expirydate = $("#datepickerid").val(); can i use this value via the ajax call ? 我可以通过ajax调用使用此值吗?

Yes, you can use the `var expirydate = $("#datepickerid").val()' in ajax call. 是的,您可以在ajax调用中使用`var expirydate = $(“#datepickerid”)。val()'。

For demo ( fiddle ), I have created a property in Model as 对于演示( 小提琴 ),我在Model中创建了一个属性为

[DataType(DataType.Date)]
public DateTimeOffset? ExpiryDate { get; set; }

And rendered input as : 并将输入呈现为:

@Html.EditorFor(model => model.ExpiryDate, new {@class="form-control"})

on click of button press, following ajax call is made : 单击按钮时,将进行以下ajax调用:

$.ajax({
    url: '@Url.RouteUrl(new{ action="YourAction", controller="YourController"})',
    data: {datetimeOffset: $('#ExpiryDate').val()},     type: 'POST',                                   success: function(resp) {
    <<Your code>>
}});

I have created this dotnet fiddle to demonstrate your ask. 我创建了这个 dotnet小提琴来演示您的要求。 Please have a look into it. 请看看。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM