c# - 为什么隐藏字段值没有发布在 mvc 4 中?

标签 c# javascript .net asp.net-mvc asp.net-mvc-4

在我的 MVC-4 应用程序中,我使用 javaScript 从地理定位中获取位置,并在隐藏字段中设置纬度、经度和精度。值在隐藏字段中设置正确,但在回发期间显示为空。

这是我的代码:

Razor :

@using (Html.BeginForm()
{
     @Html.HiddenFor(x => x.CurrentLocation.Latitude)
     @Html.HiddenFor(x => x.CurrentLocation.Longitude)
     @Html.HiddenFor(x => x.CurrentLocation.Accuracy)
     <input type="submit" name="command" value="Start" />
}

JavaScript:

function SetLocation(position) {
    console.log("{0},{1},{2}".format(position.coords.latitude, position.coords.longitude, position.coords.accuracy));
    $("#CurrentLocation_Latitude").val(position.coords.latitude);
    $("#CurrentLocation_Longitude").val(position.coords.longitude);
    $("#CurrentLocation_Accuracy").val(position.coords.accuracy);
}

$(document).ready(function () {
    $('form').submit(function () {
        LocationService.getCurrentLocation(SetLocation);
    });
});

In Html Inside My Controller

但是如果我写这段代码就可以了

  $(document).ready(function () {
    $('form').submit(function () {
         $("#CurrentLocation_Latitude").val(100); 
        $("#CurrentLocation_Longitude").val(100);
    });
});

这会向 Controller 发送正确的值。

enter image description here

我不明白为什么会这样。

提前致谢。

更新 1:

型号:

[ComplexType]
public class Location
{
    public Location()
    {

    }
    public Location(double latitude, double longiture, double? accuracy = null)
    {
        Latitude = latitude;
        Longitude = longiture;
        if (accuracy.HasValue)
            Accuracy = accuracy.Value;
    }

    [DisplayName("Latitude : ")]
    public double? Latitude { get; set; }
    [DisplayName("Longitude : ")]
    public double? Longitude { get; set; }
    public double? Accuracy { get; set; }
}


public class ServiceInfoEditMetadata : ServiceInfo
 {
    public Int64 MachineId { get; set; }

    [DisplayName("Client Name :")]
    public string ClientName { get; set; }

    [DisplayName("Site Name :")]
    public string SiteName { get; set; }
    public Location CurrentLocation { get; set; }

    [DisplayName("Client Username :")]
    public string ClientUsername { get; set; }

    [DisplayName("Client Password :")]
    public string ClientPassword { get; set; }
}

Controller :

 public ActionResult Edit(Int64 id, ServiceInfoEditMetadata serviceInfoEditMetadata, string command)
    {
        try
        {
            switch (command)
            {
                case "Add":
                    AddMachineToServiceInfoDetails(serviceInfoEditMetadata);
                    return View(serviceInfoEditMetadata);
                case "Start":
                    _serviceInfoService.StartService(serviceInfoEditMetadata, User.Identity.Name);
                    return RedirectToAction("Edit", serviceInfoEditMetadata.Id);
                case "Update":
                    if (!ModelState.IsValid) return View(serviceInfoEditMetadata);
                    _serviceInfoService.UpdateServiceInfo(serviceInfoEditMetadata, User.Identity.Name);
                    return RedirectToAction("List");
            }
            return View(serviceInfoEditMetadata);
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("ServiceInfoEditError", ex.Message);
            return View(serviceInfoEditMetadata);
        }
    }

最佳答案

我假设您的 LocationService.getCurrentLocation 是异步的。然后,您应该延迟提交表单,直到它完成。

也许你可以试试:

$(document).ready(function () {
    var locationSet =false;
    $('form').submit(function (event) {
        if(!locationSet)
             event.preventDefault();
        LocationService.getCurrentLocation(
            function(position){
                  SetLocation(position);
                  locationSet= true;
                  $('form').submit();
             }
         );
    });
});

关于c# - 为什么隐藏字段值没有发布在 mvc 4 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20345675/

相关文章:

javascript - Awesomium 点击标签 C#

javascript - 将小容器放入大容器中,如果大容器一层有2个小容器(见图)

asp.net - 将不同的用户重定向到另一个 URL

c# - 如何将 EDMX 嵌入 Code First 程序集中?

c# - 如何在 Entity Framework Core 2 中播种?

javascript - ngBindTemplate 和 ngBind 有什么区别?

javascript 闭包和原型(prototype)

.net - 在 Entity Framework 中防止if-exists-update-else-insert的竞争条件

.net - 如何为 MSHTML v9 编译 .dll。我目前收到 100 多个 "MIDL 2035: constant expression expected"错误

c# - 在 C# 中解析带有标题的 CSV 文件