【问题标题】:How to Show the value in textbox based on selection of item from dropdown list in mvc4?如何根据从 mvc4 的下拉列表中选择项目在文本框中显示值?
【发布时间】:2016-02-04 10:52:26
【问题描述】:

如何根据下拉列表中的选定值更新我的文本框以显示 Contact 模型的 Email1Mobile1 属性

查看

@Html.LabelFor(model => model.CustomerName , new { @class = "control-label" })
@Html.DropDownListFor(model => model.CustomerContactID, new SelectList(string.Empty, "Value", "Text"), "Please select a ContactPerson", new { @style = "width:250px;" })

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

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

脚本

$('#CustomerContactID').change(function () {
    $('#ContactID').empty();
    $.ajax({
        type: "GET",
        url: "/VisitorsForm/GetEmailByCustomerContactId",
        datatype: "Json",
        data: { CustomerContactID: $('#CustomerContactID').val() },
        success: function (data) {
            $('#Email1').val(data.Email1);
            $('#MobileNo1').val(data.Mobile1);
        }
    });
});

控制器

public JsonResult GetEmailByCustomerContactId(string CustomerContactId)
{
    Guid Id = Guid.Parse(CustomerContactId);
    var contacts = from a in db.Contacts where a.ContactID == Id select a;
    return Json(contacts);
}

【问题讨论】:

  • 什么不起作用?你得到什么错误? var contacts = from a in db.Contacts where a.ContactID == Id select a; return Json(contacts); 返回一个集合,而一个集合没有名为 Email1Mobile1 的属性。建议你删除90%与你的问题无关的代码,专注于问题,然后你就会得到答案。
  • 其实email和MobileNo在联系表中
  • 我没有收到任何错误
  • 你不明白我的最后评论吗(我知道 Email 和 MobileNo 在联系人表中 - 这无关紧要)
  • 答案帮助@Vani

标签: c# jquery asp.net-mvc-4


【解决方案1】:

您在GetEmailByCustomerContactId() 中的查询返回IEnumerable<Contact>,一个集合,而不是单个对象,因此ajax 成功回调中的data 是一个数组,而$('#Email1').val(data.Email1); 失败,因为数组没有名为的属性Email1(但集合中的每个项目都有)

由于您只想返回一个Contact,请将您的查询更改为

var contact = (from a in db.Contacts where a.ContactID == Id select a).FirstOrDefault();

并且由于您只需要 Contact 的 2 个属性,因此返回一个仅包含这些属性的匿名对象(通过从不使用的线路发送数据不会降低性能

var data = new { Email1 = contact.Email1, Mobile1 = contact.Mobile1 };

最后指定 JsonRequestBehavior 选项,因为您进行了 GET 调用

return Json(data, JsonRequestBehavior.AllowGet;);

接下来,您不会使用id="Email1"id="Mobile1" 生成任何输入。您创建的两个文本框都绑定到ContactID,所以我认为这些应该是

@Html.TextBoxFor(model => model.Email1, new { @class = "form-control" })
@Html.TextBoxFor(model => model.Mobile1, new { @class = "form-control" })

假设Email1Mobile1 也是视图中模型的属性。

您的文本框现在将在 success 回调中更新

旁注:

  1. 始终使用url: '@Url.Action(....)', 以确保正确的网址是 生成
  2. 使用data: { CustomerContactID: $(this).val() },
  3. 将方法参数更改为public JsonResult GetEmailByCustomerContactId(Guid CustomerContactId) 并删除 Guid.ParseDefaultModelBinder 将进行转换)
  4. 从您的 Html.TextBoxFor() 方法中删除 new { type = "text" } (助手已经添加了)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-04
    • 2021-07-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多