Reputation: 6541
I'm creating an auction. The Auction object has lots of Objects as properties such as:
Auction
-[Property]
-[Neighborhood]
-[Agent]
-source
-clientId
-reserve
Property
-Street
-City
-State
-Zip
Agent
-Name
-Email
-Telephone
Neighborhood
-Name
-HOA Fee
So basically I have to create a multiple tab view to update the Auction object.
Tabs are: Auction, Property, Agent, Neighborhood
Unfortunately I've been asked to put properties from other Object in the tabs. So or example the Neighborhood tab will have the Property.Zip and the Property tab will have the Auction.ClientId.
So what I did was create a partial for each tab in a view.. a simplified example below.
@Model Auction
@using(Html.BeginForm("Update", "Auction", HttpMethod.Post, new {})
{
<div class="panel">
@Html.Partial("_AuctionTab")
</div>
<div class="panel">
@Html.Partial("_PropertyTab")
</div>
<div class="panel">
@Html.Partial("_AgentTab")
</div>
<div class="panel">
@Html.Partial("_NeighborhoodTab")
</div>
<button>Submit</button>
}
The partials are all using the Auction Model.
Here's an example of the _PropertyTab partial:
@Model Auction
@Html.LabelFor(m=> m.ClientId)<br/>
@Html.EditorFor(m=> m.ClientId)<br/><br/>
@Html.LabelFor(m=> m.Property.Street)<br/>
@Html.EditorFor(m=> m.Property.Street)<br/><br/>
@Html.LabelFor(m=> m.Property.City)<br/>
@Html.EditorFor(m=> m.Property.City)<br/><br/>
@Html.LabelFor(m=> m.Property.State)<br/>
@Html.EditorFor(m=> m.Property.State)<br/><br/>
@Html.LabelFor(m=> m.Property.Zip)<br/>
@Html.EditorFor(m=> m.Property.Zip)<br/><br/>
@Html.LabelFor(m=> m.Property.Street)<br/>
@Html.EditorFor(m=> m.Property.Street)<br/><br/>
The problem is that when I click submit, the Auction returned to the Update action is mostly null aside from the values of the first partial. I'm sort of banging my head against the wall here as far as what to do.
Basically the submit button has to save the entire Auction. Anything suggestions of approach and best practice here?
Thanks!
Upvotes: 1
Views: 279
Reputation: 13767
First of all, when you call the partial view you need to set the Model property like this:
@Html.Partial("_AuctionTab", Model)
On the other hand, I recommend you to define ViewModels instead of working with business entities. It's gonna give you much more flexibility and you don't need to make partial views that depend on Auction. Also, if then you are asked to have a calculated value or a property that's not part of the Auction object, you can do it easily. You should build the ViewModel object in the controller.
Upvotes: 0
Reputation: 17680
The model binding isn't helping you for Navigational properties like Property, Agent etc.
You could use a ViewModel to achieve this instead.
And your Controller would take the viewmodel as parameter instead of the Auction object.
Public class AuctionViewModel
{
public int ClientId {get; set;}
public string Street {get; set;}
//the rest of them.
}
Your View would now use @model AuctionViewModel
instead of Auction.
You query would specifically do a projection to retrieve an AuctionViewModel
instead of an Auction.
Upvotes: 1