简体   繁体   English

为什么隐藏字段值不会在mvc 4中发布?

[英]Why Hidden Fields value are not posted in mvc 4?

In my MVC-4 application i take location from geolocation using javaScript and set latitude, longiture and accuracy in Hidden Fields. 在我的MVC-4应用程序中,我使用javaScript从地理位置获取位置,并在隐藏字段中设置纬度,长度和精度。 Values are set correctly in Hidden fields but during PostBack it shows null. 值在隐藏字段中正确设置,但在PostBack期间显示为null。

Here is my code : 这是我的代码:

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 : 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);
    });
});

在Html中我的控制器里面

But if I write this code then it`s work 但是,如果我写这个代码,那么它的工作

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

This send proper value to the controller. 这会向控制器发送适当的值。

在此输入图像描述

I don`t understand why this is happening. 我不明白为什么会这样。

Thank`s in advance. 提前谢谢。

Update 1 : 更新1:

Model : 型号:

[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);
        }
    }

I assume your LocationService.getCurrentLocation is asynchronous. 我假设您的LocationService.getCurrentLocation是异步的。 You should then delay form submission until it completes. 然后,您应该延迟表单提交,直到完成。

maybe you can try : 也许你可以尝试:

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

There could be more causes, because the mechanism (eg mvc binding, ajax) of passing the fields' data is little complex. 可能有更多原因,因为传递字段数据的机制(例如mvc绑定,ajax)并不复杂。

I see that in the following js code you're passing handler to function 'SetLocation'. 我在下面的js代码中看到你将处理程序传递给函数'SetLocation'。 Try to invoke the function: 尝试调用该函数:

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

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

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