MVC内部联接Linq(初学者问题)

时间:2011-02-07 20:36:14

标签: asp.net-mvc asp.net-mvc-3

你好我有这个控制器(可能我弄错了)

 public ActionResult Detail(int id)
{

    var detail = from cities in _db.Cities
                 join properties in _db.Properties on cities.CityId equals properties.CityId
                 join proplocations in _db.PropLocations on properties.LocationId equals proplocations.LocationId
                 join proptypes in _db.PropTypes on properties.TypeId equals proptypes.TypeId
                 where properties.PropId == id
                 select new
                 {
                     cities.CityName,
                     proptypes.PropType1,
                     proplocations.Location,
                     properties.PropName,
                     properties.PropOwner,
                     properties.PropStars,
                     properties.PropAddress,
                     properties.PropDescription,
                     properties.MaxGuests,
                     properties.PropConditions,
                     properties.PropId

                 };       


    return View(detail.FirstOrDefault());

}

和这个EDMX: model

有人可以解释一下如何获取属性视图的详细信息吗?我试过差异组合,但由于我在该领域的经验不足(这是我的第一次尝试)没有任何效果。

请帮助我使这个视图有效 谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用Html.DisplayForModel()替换所有标记代码:)

如需进一步参考,请检查this

此外,你为什么要使用所有这些连接?您可以使用导航属性访问相关的表/实体。

答案 1 :(得分:1)

最后找到了答案:

 @model accomm2.Models.Property

@{
    ViewBag.Title = "Detail";
}

<h2>Detail</h2>

<fieldset>
    <legend>Property</legend>

    <div class="display-label">CityId</div>
    <div class="display-field">@Model.City.CityName</div>

    <div class="display-label">TypeId</div>
    <div class="display-field">@Model.PropType.PropType1</div>

    <div class="display-label">LocationId</div>
    <div class="display-field">@Model.PropLocation.Location</div>

    <div class="display-label">PropName</div>
    <div class="display-field">@Model.PropName</div>

    <div class="display-label">PropOwner</div>
    <div class="display-field">@Model.PropOwner</div>

    <div class="display-label">PropStars</div>
    <div class="display-field">@Model.PropStars</div>

    <div class="display-label">PropAddress</div>
    <div class="display-field">@Model.PropAddress</div>

    <div class="display-label">PropPhone</div>
    <div class="display-field">@Model.PropPhone</div>

    <div class="display-label">PropEmail</div>
    <div class="display-field">@Model.PropEmail</div>

    <div class="display-label">PropWebsite</div>
    <div class="display-field">@Model.PropWebsite</div>

    <div class="display-label">PropDescription</div>
    <div class="display-field">@Model.PropDescription</div>

    <div class="display-label">MaxGuests</div>
    <div class="display-field">@Model.MaxGuests</div>

    <div class="display-label">PropConditions</div>
    <div class="display-field">@Model.PropConditions</div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.PropId }) |
    @Html.ActionLink("Back to List", "Index")
</p>