使用javascript设置时未传递给控制器​​的强类型隐藏字段的值

时间:2017-06-12 11:34:11

标签: javascript asp.net-mvc razor

我有以下观点:

<div class="form-wrap routeDetails">

    @Html.HiddenFor(newOrder => newOrder.ID)
    @Html.HiddenFor(newOrder => newOrder.WebUserID)
    @Html.HiddenFor(newOrder => newOrder.CustomerID)
    @Html.HiddenFor(newOrder => newOrder.UnregisteredCustomerID)
    @Html.HiddenFor(newOrder => newOrder.Address)
    @Html.HiddenFor(newOrder => newOrder.AddressNo)
    @Html.HiddenFor(newOrder => newOrder.ZipCode)
    @Html.HiddenFor(newOrder => newOrder.Area)
    @Html.HiddenFor(newOrder =>  newOrder.Latitude)
    @Html.HiddenFor(newOrder => newOrder.Longitude)

    <div class="center" style="margin-right:51px">
        <img id="phone" width="23px" height="23px" src="@Url.Content("~/Content/images/phone.png")" />
        @Html.TextBoxFor(newOrder => newOrder.Phone, new { @class = "field", style = "width: 300px;margin-left:10px", maxlength = 20 })
    </div>

    <br><br>

    <div class="rcorners">

        <img id="customer" width="23px" height="23px" src="@Url.Content("~/Content/images/customer.png")" style="padding-right:11px"/>
        @Html.TextBoxFor(newOrder=>newOrder.Name, new { @class = "field", style = "width: 300px;", maxlength = 50 })
        <br><br>

        <img id="home" width="23px" height="23px" src="@Url.Content("~/Content/images/home.jpg")" style="padding-right:11px" />

       @* @Html.TextBoxFor(newOrder => newOrder.Address, new { @class = "field", @id= "Address", style = "width: 300px;", maxlength = 150 })*@
          @Html.TextBox("autocomplete")
        <br><br>

        <img id="delivComments" width="23px" height="23px" src="@Url.Content("~/Content/images/comment.png")" style="padding-right:11px" />
        @Html.TextBoxFor(newOrder => newOrder.DeliveryInstructions, new { @class = "field", style = "width: 300px;", maxlength = 500 })
        <br><br>

        @Html.Kendo().CheckBoxFor(newOrder=>newOrder.saveCust).Label("Αποθήκευση στοιχείων πελάτη για μελλοντική χρήση")

    </div>

    <br><br>

    <div class="rcorners2">

        <img id="value" width="23px" height="23px" src="@Url.Content("~/Content/images/value.png")" style="padding-right:3px"/>
        @Html.Kendo().NumericTextBoxFor(newOrder => newOrder.Value).HtmlAttributes(new { @class = "Value dis", style = "width: 90px;" }).Min(0)

        <br><br>
        <img id="comments" width="23px" height="23px" src="@Url.Content("~/Content/images/comment.png")" style="padding-right:11px"/>
        @Html.TextBoxFor(newOrder => newOrder.Comments, new { @class = "field", style = "width: 300px;height: 50px",  maxlength = 500 })

    </div>
    <br><br>


</div>


<script type="text/javascript">

$(document).ready(function () {

    var options = {
        types: ['geocode'],
        region: ['address'],
        componentRestrictions: { country: "GR" }
    };

    var input = document.getElementById('autocomplete');
    var places = new google.maps.places.Autocomplete(input, options);

    places.addListener('place_changed', function () {
        var place = places.getPlace();
        for (var i = 0; i < place.address_components.length; i++) {
            for (var j = 0; j < place.address_components[i].types.length; j++) {

                if (place.address_components[i].types[j] == "postal_code") {
                    document.getElementById('ZipCode').value = place.address_components[i].long_name;
                }

                else if (place.address_components[i].types[j] == "route") {
                    document.getElementById('Address').value = place.address_components[i].long_name;
                }

                else if (place.address_components[i].types[j] == "street_number") {
                    document.getElementById('AddressNo').value = place.address_components[i].long_name;
                }

                else if (place.address_components[i].types[j] == "locality") {
                    document.getElementById('Area').value = place.address_components[i].long_name;
                }

            }
        }

        document.getElementById('Latitude').value =    place.geometry.location.lat();
        document.getElementById('Longitude').value = place.geometry.location.lng();
    });



});

当我使用javascript设置隐藏字段的值时,我实际上可以看到 值集但它没有传递给控制器​​,并且没有传递我的意思是它是null。另一方面,如果我在文本框上手动设置它们,它们被传递正常。我检查了谷歌api带来的值和它们被返回了。只是传递的值是null。为什么会发生这种情况?

控制器的方法如下所示:

Public ActionResult ctlOrders_Create([DataSourceRequest]DataSourceRequest request, OrdersViewModel newOrder, int userID)
    {
        if (ModelState.IsValid)
        {//do stuff}
    }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CityLog.Models;

namespace CityLog.ViewModels
{
    public class OrdersViewModel
    {

        //Order attributes
        public int ID { get; set; }
        public Nullable<decimal> Value { get; set; }
        public Nullable<int> WebUserID { get; set; }
        public Nullable<int> CustomerID { get; set; }
        public Nullable<int> UnregisteredCustomerID { get; set; }
        public string Comments { get; set; }
        public Nullable<int> BackendUserID { get; set; }
        public Nullable<int> CallId { get; set; }
        public Nullable<DateTime> inserted_at { get; set; }

        //Στοιχεία ντελιβερά
        public Nullable<int> DeliverymanID { get; set; }
        public string deliveryManName { get; set; }
        public string deliveryManPhone { get; set; }

        //Customer attributes

        public string custName { get; set; }
        public string Phone { get; set; }
        public Nullable<decimal> Latitude { get; set; }
        public Nullable<decimal> Longitude { get; set; }
        public string custAddress { get; set; }
        public string DeliveryInstructions { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string AddressNo { get; set; }
        public string ZipCode { get; set; }
        public string Area { get; set; }

        public Boolean saveCust { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题位于代码的JavaScript位,这是因为您假设隐藏字段id应该与您引用的一样。

除非你已经覆盖了他们的默认id剃须刀,否则假设你想要使用它。在您的情况下,应该如下所示:

 if (place.address_components[i].types[j] == "postal_code") {
                    document.getElementById('OrdersViewModel_ZipCode').value = place.address_components[i].long_name;
                }

                else if (place.address_components[i].types[j] == "route") {
                    document.getElementById('OrdersViewModel_Address').value = place.address_components[i].long_name;
                }

                else if (place.address_components[i].types[j] == "street_number") {
                    document.getElementById('OrdersViewModel_AddressNo').value = place.address_components[i].long_name;
                }

                else if (place.address_components[i].types[j] == "locality") {
                    document.getElementById('OrdersViewModel_Area').value = place.address_components[i].long_name;
                }