2014-03-03 119 views
0

使用Json填充文本框的值來自數據庫的某些文本框。要讓事件觸發,用戶必須從下拉列表中選擇一個電子郵件地址。然後用必要的值填充文本框。下面是我的代碼,但它不返回任何東西,如果即時通訊運行它。基於在mvc4中選擇下拉列表值來填充文本框

JQUERY

$(function() { 
     GetUserInfo($("#EmailAddress")); 
    }); 

    function GetUserInfo(e) { 
     var EmailId = $(e).val(); 
     console.log(EmailId); 
     $.ajax({ 
      url: '@Url.Action("GetUserInfo", "Permit")', 
     dataType: 'json', 
     type: 'POST', 
     data: { EmailAddress: EmailId }, 
     success: function (msg) { 
      $("#FirstName").attr("value", msg); 
      $("#LastName").attr("value", msg); 
      $("#MobileNumber").attr("value", msg); 
     }, 
     error: function() { } 
    }); 
} 

HTML

<div class="row-fluid"> 
    <div class="span6"> 
    <p class="input-wrap"> 
    @Html.Label("Select Email Address") 
    @Html.DropDownListFor(m=>Model.contactInfo.EmailAddress, new SelectList (Model.AllEmployeeEmail,"Value","Text"),"----select One----", new { @class = "required span12", onchange = "getEmail(this);"}) 
    @Html.ValidationMessageFor(m => Model.contactInfo.EmailAddress) 
    </div> 
    <div class="span6"> 
    </div> 
</div> 

<div class="row-fluid"> 
    <div class="span6"> 
    <p class="input-wrap"> 
    @Html.LabelFor(m => Model.contactInfo.FirstName) 
    @Html.TextBoxFor(m => Model.contactInfo.FirstName, new { @class = "required span12" }) 
    @Html.ValidationMessageFor(m => Model.contactInfo.FirstName) 
    </div> 
    <div class="span6"> 
    </div> 
</div> 

<div class="row-fluid"> 
    <div class="span6"> 
    <p class="input-wrap"> 
    @Html.LabelFor(m => Model.contactInfo.LastName) 
    @Html.TextBoxFor(m => Model.contactInfo.LastName, new { @class = "required span12" }) 
    @Html.ValidationMessageFor(m => Model.contactInfo.LastName) 
    </div> 
    <div class="span6"> 
    </div> 
</div> 

<div class="row-fluid"> 
    <div class="span6"> 
    <p class="input-wrap"> 
    @Html.LabelFor(m => Model.contactInfo.MobileNumber) 
    @Html.TextBoxFor(m => Model.contactInfo.MobileNumber, new { @class = "required span12" }) 
    @Html.ValidationMessageFor(m => Model.contactInfo.MobileNumber) 
    </div> 
    <div class="span6"> 
    </div> 
</div> 

控制器

[HttpPost] 
public JsonResult GetUserInfo(string Email) 
{ 
    // ContactInfo info = new ContactInfo(); 
    var info = from ci in db.ContactInfo 
       join cci in db.CompanyContactInfo on ci.Id equals cci.ContactInfoId 
       join cr in db.CompanyReg on cci.CompanyRegId equals cr.Id 
       where cr.CompanyRegCode == User.Identity.Name 
       select new SelectListItem() { Value = ci.Id.ToString(), Text = ci.EmailAddress }; 

    return Json(info, JsonRequestBehavior.AllowGet); 
} 

請幫助我。我知道我錯過了什麼。我只是不知道它是什麼。謝謝。

回答

0

您應該提供與服務器代碼中的參數相同的發佈數據。在上述情況下,你應該給電子郵件,而不是EmailAddress的

function GetUserInfo(e) { 
    var EmailId = $(e).val(); 
    console.log(EmailId); 
    $.ajax({ 
     url: '@Url.Action("GetUserInfo", "Permit")', 
    dataType: 'json', 
    type: 'POST', 
    data: { Email: EmailId }, 
    success: function (msg) { 
     $("#FirstName").attr("value", msg); 
     $("#LastName").attr("value", msg); 
     $("#MobileNumber").attr("value", msg); 
    }, 
    error: function() { } 
}); 
+0

我仍然有同樣的問題。 –

+0

你會得到任何控制檯錯誤? –

+0

@HarunaAdo在ajax的成功事件中發出警報。檢查它是否發生。 –

1

改變這種

data: { EmailAddress: EmailId },

在jQuery的POST

data: { Email: EmailId },

,因爲你的行動GetUserInfo接受一個名爲「Email」的字符串參數,而不是「EmailAddress」。兩者應該是一樣的。

相關問題