2016-03-05 86 views
0

您好我有我的觀點CustomerNameContactPersonEmail四個字段,MobileNo如何在mvc4中使用json返回多個參數?

CustomerNameContactPerson被級聯下拉列表和EmailMobileNo是文本框。

如果我選擇CustomerName相關ContactPerson會自動加載下載ContactPerson下載。

如果我選擇ContactpersonContactPerson相關EmailPhoneNo將在EmailPhoneNo文本框自動加載。我完成了所有任務,所有工作都正常。現在我需要的是我希望減少代碼。

我的控制器代碼

public JsonResult GetCustomers() 
    { 
     return Json(db.Customers.ToList(), JsonRequestBehavior.AllowGet); 
    } 

    public JsonResult GetContactPersobByCustomerId(string customerId) 
    { 
     Guid Id = Guid.Parse(customerId); 
     var customercontacts = (from a in db.CustomerContacts where a.CustomerID == Id select a); 
     return Json(customercontacts, JsonRequestBehavior.AllowGet); 
    } 

    public JsonResult GetEmailByContactPersonID(Guid CustomerContactId) 
    { 
     var ContactID = db.CustomerContacts.Where(i => i.CustomerContactID == CustomerContactId).Select(i => i.ContactID).FirstOrDefault(); 
     var contact1 = (from p in db.Contacts where p.ContactID == ContactID select p).FirstOrDefault().Email1; 
     if (contact1 == null) 
     { 
      var contact2 = (from a in db.Contacts where a.ContactID == ContactID select a).FirstOrDefault().Email2; 
      contact1 = contact2; 
     } 
     return Json(contact1, JsonRequestBehavior.AllowGet); 
    } 

public JsonResult GetPhoneNoByContactPersonID(Guid CustomerContactId) 
    { 
     var ContactID = db.CustomerContacts.Where(i => i.CustomerContactID == CustomerContactId).Select(i => i.ContactID).FirstOrDefault(); 
     var mobile1 = (from pn in db.Contacts where pn.ContactID == ContactID select pn).FirstOrDefault().Mobile1; 
     if (mobile1 == null) 
     { 
      var mobile2 = (from a in db.Contacts where a.ContactID == ContactID select a).FirstOrDefault().Mobile2; 

      mobile1 = mobile2; 
     } 
     return Json(mobile1, JsonRequestBehavior.AllowGet); 
    } 

我查看代碼

@Html.Label("Customer Name", new { @class = "control-label" }) 
    @Html.DropDownListFor(model => model.CustomerID, new SelectList(string.Empty, "Value", "Text"), "Please select a Customer", new { @class = "form-control required", type = "text" }) 

    @Html.Label("Contact Person", new { @class = "control-label" }) 
    @Html.DropDownListFor(model => model.CustomerContactID, new SelectList(string.Empty, "Value", "Text"), "Please select a ContactPerson", new { @class = "form-control", type = "text", id = "CustomerContactID" }) 

    @Html.LabelFor(model => model.MobileNo, new { @class = "control-label" }) 
    @Html.TextBoxFor(model => model.MobileNo, new { @class = "form-control", type = "text",disabled = "disabled", @readonly = "readonly" }) 
    @Html.ValidationMessageFor(model => model.MobileNo) 

    @Html.LabelFor(model => model.Email, new { @class = "control-label" }) 
    @Html.TextBoxFor(model => model.Email, new { @class = "form-control", type = "text" ,disabled = "disabled", @readonly = "readonly" }) 
    @Html.ValidationMessageFor(model => model.Email) 

的json代碼

<script src="~/Scripts/jquery-ui-1.11.0.js"></script> 
    <script> 
     $(function() { 
     $.ajax(
     '@Url.Action("GetCustomers", "VisitorsForm")',{ 
      type: "GET", 
      datatype: "Json", 
      success: function (data) { 
       $.each(data, function (index, value) { 
        $('#CustomerID').append('<option value="' + value.CustomerID + '">' + value.DisplayName + '</option>'); 
       }); 
      } 
     }); 

      $('#CustomerID').change(function() { 
     $('#CustomerContactID').empty(); 

     $.ajax(
      '@Url.Action("GetContactPersobByCustomerId", "VisitorsForm")',{ 
       type: "POST", 
       datatype: "Json", 
       data: { CustomerID: $('#CustomerID').val() }, 
       success: function (data) { 
      $('#CustomerContactID').append($('<option></option>').val('').text('Please select')); 
        $.each(data, function (index, value) { 
       $('#CustomerContactID').append('<option value="' + value.CustomerContactID + '">' + value.ContactReference + '</option>'); 
        }); 
        } 
       }); 
      }); 
     }); 

    $("#CustomerContactID").change(function() { 
      alert("hhh"); 
    $.ajax(
     '@Url.Action("GetEmailByContactPersonID", "VisitorsForm")',{ 
      type: "GET", 
      dataType: "json", 
      async: false, 
      data: { CustomerContactID: $("#CustomerContactID").val() 
      }, 
      error: function (ex) { 
       alert('Failed to retrieve Email.' + ex); 
      }, 
      beforeSend: function() { 
      }, 
      success: function (data) { 
       $("#Email").val(data); 
          } 
         }); 
        }); 

    $("#CustomerContactID").change(function() { 
    alert("hhh"); 
    $.ajax(
     '@Url.Action("GetPhoneNoByContactPersonID", "VisitorsForm")',{ 
      type: "GET", 
      dataType: "json", 
      async: false, 
      data: { CustomerContactID: $("#CustomerContactID").val() 
      }, 
      error: function (ex) { 
       alert('Failed to retrieve Email.' + ex); 
      }, 
      beforeSend: function() { 
      }, 
      success: function (data) { 

       $("#MobileNo").val(data); 
      } 
      }); 
     }); 

小號ee我的編碼我寫了單獨的json和jquery,代碼爲EmailPhoneNo。我覺得它不好。我希望縮短我的代碼。我想使用json傳遞多個參數。我想通過Emailphone,同時選擇ContactPerson下拉菜單中的ContatcPerson。它也影響我的應用程序的性能。請任何人爲我的問題提供解決方案和建議。

提前謝謝..

+0

你只需要一個'$( 「#CustomerContactID」)改變(函數(){'它調用一個控制器方法(說)'GetContactPersonDetails() '返回兩個值作爲匿名對象(請參閱Andreas Pilavakis的回答),然後使用$(「#Email」).val(data.email);''和$(「#Number」)。val(data.phone );' –

回答

0

你爲什麼不返回從你的控制器對象包含所有你需要的信息?

序列化爲Json的任何對象都可以作爲JsonResult返回。

你甚至可以做匿名類型,如:

var details = new { email = "[email protected]", phone = "1234567"}; 
return Json(details);