基于mvc4中的下拉列表值选择填充文本框

时间:2014-03-03 12:05:33

标签: jquery json asp.net-mvc-4

我正在使用Json填充文本框的值来自db的文本框。要触发事件,用户必须从下拉列表中选择一个电子邮件地址。然后使用必要的值填充文本框。下面是我的代码,但如果我正在运行它,它不会返回任何内容。

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> 

CONTROLLER

[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);
} 

请帮帮我。我知道我错过了什么。我只是不知道它是什么。谢谢。

2 个答案:

答案 0 :(得分:1)

更改此

data: { EmailAddress: EmailId },

在你的jquery POST到

data: { Email: EmailId },

因为您的操作GetUserInfo接受名为“Email”而不是“EmailAddress”的字符串参数。两者都应该相同。

答案 1 :(得分: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 () { }
});