MVC Inner join Linq (begginner question)
Hello i have this controller(probably i got it wrong)
public ActionResult Detail(int id)
{
var detail = from cities in _db.C开发者_如何转开发ities
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());
}
and this EDMX:
could somebody explain me please how to get the detail of the property view? i tried difference combination but because of my low experience in the field (this is my first try) nothing worked.
Please help me to make this view work thank you
You could've replaced all that markup code with Html.DisplayForModel() :)
For further reference, check this out.
Moreover, why are you using all these joins? You could use navigational properties to access related tables/entities.
Just found the answer finally :
@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>
精彩评论