C# 级联文本框在MVC4中工作不正常?
您好,我的视图中有四个字段CustomerName,ContactPerson,Email,MobileNo CustomerName和ContactPerson是层叠式下拉列表,Email和MobileNo是文本框 如果我选择CustomerName,相关联系人将自动加载到ContactPerson下拉列表中 如果我选择联系人,则与联系人相关的电子邮件和电话号码将自动加载到电子邮件和电话号码文本框中。这正如预期的那样有效 现在一切正常两个级联下拉列表正常现在我的问题是如果我选择联系人,联系人相关的电子邮件和电话号码不会显示在相应的文本框中 我的控制器代码:C# 级联文本框在MVC4中工作不正常?,c#,jquery,ajax,asp.net-mvc-4,C#,Jquery,Ajax,Asp.net Mvc 4,您好,我的视图中有四个字段CustomerName,ContactPerson,Email,MobileNo CustomerName和ContactPerson是层叠式下拉列表,Email和MobileNo是文本框 如果我选择CustomerName,相关联系人将自动加载到ContactPerson下拉列表中 如果我选择联系人,则与联系人相关的电子邮件和电话号码将自动加载到电子邮件和电话号码文本框中。这正如预期的那样有效 现在一切正常两个级联下拉列表正常现在我的问题是如果我选择联系人,联系人相
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 GetPhoneNoByContactPersonID(Guid CustomerContactId)
{
var resultMobileNumber = string.Empty;
var resultEmail = string.Empty;
var ContactID = db.CustomerContacts.Where(i => i.CustomerContactID == CustomerContactId).Select(i => i.ContactID).FirstOrDefault();
if (ContactID != null)
{
var contact = (from p in db.Contacts where p.ContactID == ContactID select p).FirstOrDefault();
if (contact != null)
{
if (string.IsNullOrEmpty(contact.Mobile1) == false)
{
resultMobileNumber = contact.Mobile1;
}
else if (string.IsNullOrEmpty(contact.Mobile2) == false)
{
resultMobileNumber = contact.Mobile2;
}
}
if (contact != null)
{
if (string.IsNullOrEmpty(contact.Email1) == false)
{
resultEmail = contact.Email1;
}
else if (string.IsNullOrEmpty(contact.Email2) == false)
{
resultEmail = contact.Email2;
}
}
}
var details = new { success = true, email = resultEmail, mobileno = resultMobileNumber };
return Json(details, 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)
查看代码:
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 GetPhoneNoByContactPersonID(Guid CustomerContactId)
{
var resultMobileNumber = string.Empty;
var resultEmail = string.Empty;
var ContactID = db.CustomerContacts.Where(i => i.CustomerContactID == CustomerContactId).Select(i => i.ContactID).FirstOrDefault();
if (ContactID != null)
{
var contact = (from p in db.Contacts where p.ContactID == ContactID select p).FirstOrDefault();
if (contact != null)
{
if (string.IsNullOrEmpty(contact.Mobile1) == false)
{
resultMobileNumber = contact.Mobile1;
}
else if (string.IsNullOrEmpty(contact.Mobile2) == false)
{
resultMobileNumber = contact.Mobile2;
}
}
if (contact != null)
{
if (string.IsNullOrEmpty(contact.Email1) == false)
{
resultEmail = contact.Email1;
}
else if (string.IsNullOrEmpty(contact.Email2) == false)
{
resultEmail = contact.Email2;
}
}
}
var details = new { success = true, email = resultEmail, mobileno = resultMobileNumber };
return Json(details, 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)
J-query代码
<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");
debugger;
$.ajax(
'@Url.Action("GetPhoneNoByContactPersonID", "VisitorsForm")',{
type: "GET",
dataType: "html",
async: false,
data: { CustomerContactID: $("#CustomerContactID").val()
},
error: function (ex) {
alert('Failed to retrieve Email.' + ex);
},
beforeSend: function () {
},
success: function (data) {
$("#Email").val(data.email);
$("#MobileNo").val(data.mobileno)
alert("Success");
}
});
});
$(函数(){
$.ajax(
@Url.Action(“GetCustomers”、“VisitorsForm”){
键入:“获取”,
数据类型:“Json”,
成功:功能(数据){
$.each(数据、函数(索引、值){
$('#CustomerID')。追加(''+value.DisplayName+'');
});
}
});
$('#CustomerID')。更改(函数(){
$('#CustomerContactID').empty();
$.ajax(
“@Url.Action(“GetContactPersobByCustomerId”、“VisitorsForm”){
类型:“POST”,
数据类型:“Json”,
数据:{CustomerID:$('#CustomerID').val(),
成功:功能(数据){
$('#CustomerContactID')。追加($('').val('').text('请选择');
$.each(数据、函数(索引、值){
$(“#CustomerContactID”).append(“”+value.ContactReference+“”);
});
}
});
});
});
$(“#CustomerContactID”).change(函数(){
警报(“hhh”);
调试器;
$.ajax(
“@Url.Action(“GetPhoneNoByContactPersonID”、“VisitorsForm”){
键入:“获取”,
数据类型:“html”,
async:false,
数据:{CustomerContactID:$(“#CustomerContactID”).val()
},
错误:函数(ex){
警报(“检索电子邮件失败”。+ex);
},
beforeSend:函数(){
},
成功:功能(数据){
$(“#Email”).val(data.Email);
$(“#MobileNo”).val(data.MobileNo)
警惕(“成功”);
}
});
});
现在,当我单击联系人时,所有功能都正常运行。它进入GetPhoneNoByContactPersonID操作,计算值并再次返回视图,并且在网络中也可见。所有这些都很完美,但它没有在文本框中显示数据。当我检查代码时,它在控制台中没有显示任何错误。但它显示了一条警告信息,如下所述
现在一切都很好。但我不知道为什么它没有显示我不知道问题在哪里。我试着用我的bwst来解释我的问题。-请任何人帮我解决这个问题
提前感谢没有与关联的id。因此,请先在
文本框中为
指定一个id,如下所示
@Html.TextBoxFor(model => model.MobileNo, new {@id = "MobileNo", @class = "form-control", type = "text",disabled = "disabled", @readonly = "readonly" })
@Html.TextBoxFor(model => model.Email, new {@id = "Email", @class = "form-control", type = "text" ,disabled = "disabled", @readonly = "readonly" })
现在应该可以了
$("#Email").val(data.email);
$("#MobileNo").val(data.mobileno)
好的,为了进一步测试,您可以删除disabled和readonly属性,并查看是否设置了这些值。我希望数据在成功回调中有价值在控制台[![warning][2][2][2]中显示此警告:您一定是做错了什么。此警告与我的建议无关。我说的只是删除文本框中提到的只读和禁用属性以进行测试。是的,我删除了这些属性并再次检查,但仍然存在相同的问题。但是在两天前,这段代码和所有的工作都很好,只是今天它不工作。在你的问题中,文本框没有id属性。我不知道以前它是如何工作的。