MVC Inner Join via Linq
I try to create a product detail (accommodation details) view based on this controller action:
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());
}
and because is the first time i try to create this i don't know how to solve the scaffolding template model .. i get
Server Error in '/' Application.
The model item passed into the dictionary is of type '<>f__AnonymousType5`11[Syste开发者_高级运维m.String,System.String,System.String,System.String,System.String,System.Int32,System.String,System.String,System.Int32,System.String,System.Int32]', but this dictionary requires a model item of type 'accomm2.Models.Property'.
My model here:
My autogen view:
@model accomm2.Models.Property
@{ ViewBag.Title = "Detail"; }
Detail
Property<div class="display-label">CityId</div> <div class="display-field">@Model.CityId</div> <div class="display-label">TypeId</div> <div class="display-field">@Model.TypeId</div> <div class="display-label">LocationId</div> <div class="display-field">@Model.LocationId</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>
The view is strongly bound to "accomm2.Models.Property" and your "detail" object is an anonymous object, that's the reason you are getting this message.
For quick test you can try
var prop = new Property {
// Your fields goes here
}
return View(prop);
This is just a quick stub code, but ideally this conversion should happen in a separate layer.
It looks like you're trying to use LINQ projection to create a new type instead of supplying the type it is expecting--which is a "Property" object.
Either create a ViewModel and regenerate the page to use that as its strongly-typed type, or change your projection to
select new Model.Property(){ //assign vars }
Thats my guess
Your view is defined to take a Property
as a model.
You're trying to pass it an anonymous type which isn't a Property
.
You need to either select a Property
instance or remove the @model
declaration to make the view take a dynamic model.
精彩评论