ajax发布没有将所有表单值发布到控制器

时间:2016-11-02 10:57:10

标签: jquery ajax asp.net-mvc

我从中添加了一个脚本Latitude&纬度,并将其传递给textInput,然后我用ajax将它发布到控制器,这个" dynamic"文本输入在控制器端始终为空,并且在检查元素时具有值。

<script type="text/javascript">
    function GetLocation() {
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById("txtAddress").value;
        var labelvar = document.getElementById('sys');

        geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                console.log("Latitude: " + latitude + "\nLongitude: " + longitude); //Ok.

                labelvar.innerHTML = latitude + longitude; //Ok.

                $.ajax({
                    url: 'Create',
                    type: 'POST',
                    data: $('#RestoForm').serialize(),
                    datetype: "html",
                    success: function(data) {
                        $('#RestoForm').html(data);
                    }
                });

                error: function e(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            }
        });
    };
</script>
[Authorize]
public ActionResult Create()
{
    var viewModel = new LectureFormViewModel
    {
        Genres = _context.Genres.ToList(),
    };
    return View("TGigform", viewModel);
}

[Authorize, HttpPost, ValidateAntiForgeryToken]
public ActionResult Create(LectureFormViewModel viewModel)
{
    try
    {
        if (!ModelState.IsValid)
        {
            viewModel.Genres = _context.Genres.ToList();
            return View("TGigform", viewModel);
        }

        var lectureGig = new LectureGig
        {
            LatLong = viewModel.LatLong,
        };
@using (Html.BeginForm("Create", "TMaps", FormMethod.Post, new {enctype = "multipart/form-data", id = "RestoForm"}))
{
    @Html.LabelFor(m => m.LatLong)
    @Html.TextBoxFor(m => m.LatLong, new {@id = "sys"})
    <input type="button" class="btn btn-primary btn-lg" onclick="GetLocation()" value="Finish"/>
}

除了labelvar之外的所有其他值都很好并且到达控制器。 感谢

1 个答案:

答案 0 :(得分:2)

假设labelvarinput元素,您需要设置其value,而不是innerHTML。试试这个:

labelvar.value = latitude + longitude;
相关问题