【问题标题】:Why Hidden Fields value are not posted in mvc 4?为什么隐藏字段值未在 mvc 4 中发布?
【发布时间】:2013-12-03 07:56:10
【问题描述】:

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

这是我的代码:

剃须刀:

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

但是如果我写了这段代码,那么它就可以工作了

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

这会向控制器发送正确的值。

我不明白为什么会这样。

提前致谢。

更新 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; }
}

控制器:

 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(SetLocation) 周围添加一些警告语句,尤其是确定 SetLocation 位置参数是否有效的 SetLocation 方法。
  • 您的客户端和服务器之间可能存在文化设置不匹配。你的服务器文化是什么?我猜它有一个小数点分隔符是逗号而不是点的文化。因此,如果您发布值:23,70922 它应该可以工作。

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


【解决方案1】:

我假设您的 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();
             }
         );
    });
});

【讨论】:

  • 你是对的。异步调用会出现此问题。但是,如果我使用您的代码,那么它不会提交我的表单。如果我删除 event.preventDefault();然后提交我的表格,但它提交了 2 次。你能告诉我如何防止这种情况发生吗?
  • 我改成这样,its works 给我。 $(document).ready(function () { $('form').on('submit', function (e) { e.preventDefault(); var x = this; LocationService.getCurrentLocation( function (position) { SetLocation (位置);x.submit();});});});
  • @Hasan009 感谢跟进。我编辑了我的答案。随意编辑它以添加您自己的版本
【解决方案2】:

可能有更多原因,因为传递字段数据的机制(例如 mvc 绑定、ajax)并不复杂。

我看到在以下 js 代码中,您将处理程序传递给函数“SetLocation”。尝试调用函数:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多