Javascript 更新未发布,属性在绑定时具有默认值

Javascript updates not posted, properties have default value on binding

本文关键字:定时 绑定 默认值 属性 更新 Javascript      更新时间:2023-09-26

从昨天开始,我一直在为这个问题而苦苦挣扎。我有一个简单的视图模型:

public class LoginViewModel
{
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
    [Required]
    public double Longitude { get; set; }
    [Required]        
    public double Latitude { get; set; }
}

而这种形式:

<h2>@ViewBag.Title.</h2>
<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                @Html.TextBoxFor(x => x.Latitude)
                @Html.TextBoxFor(x => x.Longitude)
            }

这个JavaScript正在用用户的位置更新文本框。

<script type="text/javascript">
$(document).ready(function () {
    navigator.geolocation.getCurrentPosition(getPosition);
});
function getPosition(position) {
    document.getElementById("Latitude").value = position.coords.latitude;
    document.getElementById("Longitude").value = position.coords.longitude;
}
<script>

这是我的行动:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
       return View()
   }

真正让我发疯的是,我可以看到文本框在浏览器中正确填充了坐标,但是控制器上没有收到任何内容。所有属性:电子邮件,密码,记住等...正确绑定,但这两个。这真的很奇怪。

拜托,我真的需要解决这个问题。任何帮助将不胜感激

嗯,

我明白了。它与javascript机制无关,实际上问题在于数字的分隔符。默认情况下,javascript 用"."分隔小数部分,我不得不将其更改为","。我真的希望它能节省别人的时间。

我以前从未遇到过这个问题,但我不记得过去发布过双值。我们每天都能学到新东西。