-1
假设我要显示客户的地址和联系人号码。有一个下拉选择客户,我必须显示所选客户的地址和联系电话号码。将对象作为控制器的Json结果传递给视图
我创建了一个存储过程来返回客户地址和联系电话号码。
这是客户在视图中的下拉菜单。
<div class="form-group">
@Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.DropDownListFor(model => model.CustomerName, new SelectList(ViewBag.CustomerName, "Id", "Name"), "Select Customer", new { @class = "form-control", id = "cmbCustomerList" })
@Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
</div>
</div>
这是我到目前为止试过的脚本。
“#AddressSection”:地址和联系号码显示区域
“#cmbCustomerList”:客户下拉列表
“销售/ GetCustomerDetails”:控制器和动作
$(document).ready(function() {
//Address Section
$("#AddressSection").hide();
$('#cmbCustomerList').change(function() {
if ($('#cmbCustomerList :selected').text() == "Select Customer") {
$("#AddressSection").hide();
}
else {
$("#AddressSection").show();
var selectedVal = $(this).find(':selected').val();
$.get("/Sales/GetCustomerDetails/" + selectedVal, function (data, status) {
//Here I want to pass the values to the address and contact number labels
});
}
});
});
这是控制器中的动作
public JsonResult GetCustomerDetails(int id)
{
List<CustomerViewModel> customer = _handler.GetCustomerDetails(id);
return Json(customer, JsonRequestBehavior.AllowGet);
}
有一个在视图中重新输入3个标签以显示地址和联系人号码。
<label id="lblAddressLine1" />
<label id="lblAddressLine2" />
<label id="lblContactNumber" />
在CustomerViewModel有3个属性 AddressLine1, AddressLine2, ContactNumber
所以我的问题是如何单独视图访问这些3点的属性?
谢谢。
显示您要与客户的详细信息,以更新元素的HTML。当你只想要一个客户的细节时,为什么你要返回'List? –
在问题中,不在评论中。而你仍然没有解释为什么你返回一个集合 –