我正在尝试使用MVC标记帮助器将Bootstrap 3 Datetimepicker绑定到我的ASP.NET Core模型,如下所示:
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
}
我在初始化控件时遇到了问题。以下不起作用:
1)在Razor部分这样的脚本中:
var_dump($_POST);
if (isset($_POST['username']))
{
echo "IT IS SET!";
$username = $_POST['username'];
//Reversed around your assignments
$email = $_POST['email'];
$phone =$_POST['phone'];
$password =$_POST['password'] ;
$confirmpassword = $_POST['confirm-password'];
}
var_dump($username);
echo $password;
echo $confirmpassword;
echo $phone;
echo $email;
echo $username;
执行此操作会初始化datetimepicker,但没有模型值。如何使用模型值作为初始值?
答案 0 :(得分:2)
在视图中包含您的日期字段,如下所示:
@Html.EditorFor(model => model.MyDate, new { htmlAttributes = new { @class = "my-date" } })
并在你的JavaScript中初始化它(我正在使用jQuery)
$(document).ready(function () {
// get date from MyDate input field
var date = $(".my-date").val();
// use current date as default, if input is empty
if (!date) {
date = new Date();
}
$('.my-date').datetimepicker({
format: 'YYYY/MM/DD',
date: date
});
});
请注意我使用'my-date'类作为输入选择器,您可能希望以不同方式选择它...显然您需要包含引导程序库...
作为旁注,最好将脚本放在外部文件中:why should I avoid inline Scripts