我有以下场景,目前我从外部服务(我无法控制)检索整个人,包括他们的地址。我只想显示地址的一个实例,然后有一个模型来显示其余的(如果需要)。目前我通过这个想法然后对外部服务进行另一次查找,但是由于我已经有了数据,所以效率很低。
任何人都可以提供一个好的策略来帮助传输地址列表的最佳方式吗?
注意我以简化的形式重新输入了代码,所以请忽略任何错别字
楷模
public class PersonModel
{
Public int Id {get;set}
Public String Name {get; set;}
Public IEnumerable Address {get; set;}
// Single Address from the collection
Public DisplayAddress {get;set;}
}
public class AddressesViewModel
{
public IEnumerable<Address> Address { get; set; }
}
风景
@model PersonModel
<span>@Model.Name</span>
<ol>
@if (Model.DisplayAddress != null)
{
<li>@Model.DisplayAddress.Line1</li>
<li>@Model.DisplayAddress.Line2</li>
<li>@Model.DisplayAddress.Line3 </li>
<li>@Model.DisplayAddress.Town</li>
<li>@Model.DisplayAddress.County</li>
<li>@Model.DisplayAddress.PostalCode</li>
<li>@Model.DisplayAddress.Country</li>
}
</ol>
<a id="showalladdresses" title="View all addresses" href="#">View All Addressess</a>
@section scripts {
<script type="text/javascript">
$(function () {
$('#Modal').dialog({
autoOpen: false,
height: 600,
width: 800,
resizable: false,
modal: true
});
$('#showalladdresses').click(function () {
$('#Modal').load("@Url.Action("ViewAddresses", new { id = Model.Id })", function () {
$(this).dialog('open');
});
return false;
});
});
}
控制器
public ActionResult PersonBanner(long? Id)
{
if (!Id.HasValue)
return RedirectToAction("Index", "HomeController");
var result = new PersonModel();
result = unitOfWork.GetPersonSummaryDetails(Id.Value)
return View(result);
}
public ActionResult ViewAddresses(long Id)
{
var model = new AddressesViewModel
{
Address = unitOfWork.GetAddressesById(Id);
};
if (Request.IsAjaxRequest())
return PartialView(model);
return View(model);
}