Grid.MVC обращаясь к row.dataItem.dataItem с помощью JQuery

Я использую GridMVC для отображения списка работодателей и их адреса. ViewModel имеет список EmployerLocations, который называется AllEmpLocations, и в каждом EmployerLocation есть модель Address.

Я могу связать эту модель с сеткой, и на экране отображаются адресные строки, но когда я использую Jquery, чтобы установить значение текстового поля на основе e.row.Address.AddressLine1,

0x800a138f - Ошибка времени выполнения JavaScript: невозможно получить свойство AddressLine1 с неопределенной или нулевой ссылкой

Однако, если я проверяю e.row.Address.AddressLine1, он заполняется с помощью отладчика.

Посмотреть

   <div class="col-md-12">

                        @Html.Grid(Model.AllEmpLocations).Named("employerLocationGrid").Columns(columns =>
                    {
                        columns.Add(item => item.EmpLocationID)
                            .Titled("EmployerLocationID")
                            .Css("grid-employerID")
                            .SetWidth("20px")
                            .Sortable(true);
                        columns.Add(item => item.Name)
                            .Titled("Name")
                            .Sortable(true).Filterable(true);
                        columns.Add(item => item.areaID, true);
                        columns.Add(item => item.Address.ID, true);
                        columns.Add(item => item.Alias)
                            .Titled("Alias");
                        columns.Add(item => item.Address.NameNumber)
                            .Titled("Name/Number");
                        columns.Add(item => item.Address.AddressLine1)
                            .Titled("Address Line 1");
                        columns.Add(item => item.Address.AddressLine2)
                            .Titled("Address Line 2");
                        columns.Add(item => item.Address.PostCode)
                            .Titled("Post Code");
                        columns.Add(item => item.Address.PhoneNumber)
                            .Titled("Tel No.");
                        columns.Add(item => item.Address.Town)
                            .Titled("Town")
                            .Sortable(true).Filterable(true);
                        columns.Add(item => item.Address.County)
                            .Titled("County")
                            .Sortable(true).Filterable(true);

                    }).WithPaging(10)

                    </div>

<script>
    $(function () {

        pageGrids.employerLocationGrid.onRowSelect(function (e) {
            debugger

            //add values to textboxes from model
            $('#NewLocation_Name').val(e.row.Name); //this works
            $('#NewLocation_Address_AddressLine1').val(e.row.Address.AddressLine1); //this errors Address is    
            $('#SubmitLocationBtn').val("Update");

        });
    });
</script>

Модель AllEmpLocation

public int? EmpLocationID { get; set; }
    public string EmployerID { get; set; }
    public string EmployerName { get; set; }
    public string Alias { get; set; }
    [DisplayName("Location Name")]
    [Required()]
    public string Name { get; set; }
    public string areaID { get; set; }
    public string areaName { get; set; }
    [DisplayName("Area")]
    public IEnumerable<PlussAreaDisplayModel> AllAreas { get; set; }
    public AddressDetailsDisplayModel Address { get; set; }

Модель адреса

 public class AddressDetailsDisplayModel
{
    public string ID { get; set; }
    [DisplayName("Date From")]
    [DisplayFormat(DataFormatString = "{0:D}")]
    public DateTime? StartDate { get; set; }
    [DisplayName("Date To")]
    [DisplayFormat(DataFormatString = "{0:D}")]
    public DateTime? EndDate { get; set; }
    [DisplayName("Name/Number")]
    public string NameNumber { get; set; }
    [DisplayName("Address Line 1")]
    public string AddressLine1 { get; set; }
    [DisplayName("Address Line 2")]
    public string AddressLine2 { get; set; }
    [DisplayName("Town")]
    public string Town { get; set; }
    [DisplayName("County")]
    public string County { get; set; }
    [RegularExpression("^(([gG][iI][rR] {0,}0[aA]{2})|((([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y]?[0-9][0-9]?)|(([a-pr-uwyzA-PR-UWYZ][0-9][a-hjkstuwA-HJKSTUW])|([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y][0-9][abehmnprv-yABEHMNPRV-Y]))) {0,}[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2}))$", ErrorMessage = "Invalid UK Postcode")]
    [DisplayName("Post Code")]
    public string PostCode { get; set; }
    public string PostCodeLeft { get; set; }
    public string PostCodeRight { get; set; }
    [DisplayName("Address Phone Number")]
    public string PhoneNumber { get; set; }
    [DisplayName("Mobile Number")]
    public string MobileNumber { get; set; }
    [DisplayName("Email Address")]
    [DataType(DataType.EmailAddress)]
    [EmailAddress]
    public string EmailAddress { get; set; }
    [DisplayName("Address Type")]
    public string AddressType { get; set; }
    public string DisplayHeading { get; set; }
}
}

Спасибо за поиск

1 ответ

Хотя это, конечно, не элегантное решение, быстрое решение состоит в том, чтобы пойти и получить адрес снова, используя JSON.

$.getJSON("/Employer/GetAddressByEmpLocationID", { term: e.row.EmpLocationID },
             function (data) {
                 $('#PostCode').val(data.PostCode).prop("disabled", true).prop("readonly", "readonly");
                 $("#AddressID").val(data.ID).attr('value', data.ID);
                 $("#NewLocation_Address_NameNumber").val(data.NameNumber).prop("readonly", "readonly");
                 $("#NewLocation_Address_AddressLine1").val(data.AddressLine1).prop("readonly", "readonly");
                 $("#NewLocation_Address_AddressLine2").val(data.AddressLine2).prop("readonly", "readonly");
                 $("#NewLocation_Address_Town").val(data.Town).prop("readonly", "readonly");
                 $("#NewLocation_Address_County").val(data.County).prop("readonly", "readonly");
                 $("#NewLocation_Address_PhoneNumber").val(data.PhoneNumber).prop("readonly", "readonly");


             }, "json");   
Другие вопросы по тегам