I have a get method passing in an object of type Store to the view, upon creating the view using the menu dialog (i.e. Create a strongly-typed view) it properly scaffolds out the form when I choose the view content to be Edit. Without changing anything on the view I add a post method that accepts a store object as the parameter. The properties on the object are never populated with the form data and I am unable to figure out why. A Request.Form.Count shows 14 items which is correct minus the Id which would make 15. If I type out each parameter separately they do get set. I can also use the FormCollection to get the values, but sure would be nice to pass the entire object back in and use that.
Is there a reason why this might be happening?
Store Definition:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DomainModel
{
public class Store : GuidIdentityPersistenceBase
{
private string _address1;
private string _address2;
private string _city;
private string _county;
private int _oldStoreNumber;
private string _faxNumber;
private bool _hasHydraulicHose;
private bool _hasInsCounter;
private bool _hasPaintBooth;
private bool _isHubStore;
private bool _isOpenNightsWeekends;
private int _newStoreNumber;
private string _phoneNumber;
private string _postalCode;
private string _state;
private IList _walls = new List();
public virtual string Address1
{
get
{
return _address1;
}
set
{
_address1 = value;
}
}
public virtual string Address2
{
get
{
return _address2;
}
set
{
_address2 = value;
}
}
public virtual string City
{
get
{
return _city;
}
set
{
_city = value;
}
}
public virtual string County
{
get
{
return _county;
}
set
{
_county = value;
}
}
public virtual int OLDStoreNumber
{
get
{
return _oldStoreNumber;
}
set
{
_oldStoreNumber = value;
}
}
public virtual string FaxNumber
{
get
{
return _faxNumber;
}
set
{
_faxNumber = value;
}
}
public virtual bool HasHydraulicHose
{
get
{
return _hasHydraulicHose;
}
set
{
_hasHydraulicHose = value;
}
}
public virtual bool HasInsCounter
{
get
{
return _hasInsCounter;
}
set
{
_hasInsCounter = value;
}
}
public virtual bool HasPaintBooth
{
get
{
return _hasPaintBooth;
}
set
{
_hasPaintBooth = value;
}
}
public virtual Guid Id
{
get
{
return _persistenceId;
}
}
public virtual bool IsHubStore
{
get
{
return _isHubStore;
}
set
{
_isHubStore = value;
}
}
public virtual bool IsOpenNightsWeekends
{
get
{
return _isOpenNightsWeekends;
}
set
{
_isOpenNightsWeekends = value;
}
}
public virtual int NewStoreNumber
{
get
{
return _newStoreNumber;
}
set
{
_newStoreNumber = value;
}
}
public virtual string PhoneNumber
{
get
{
return _phoneNumber;
}
set
{
_phoneNumber = value;
}
}
public virtual string PostalCode
{
get
{
return _postalCode;
}
set
{
_postalCode = value;
}
}
public virtual string State
{
get
{
return _state;
}
set
{
_state = value;
}
}
public virtual IList Walls
{
get
{
return _walls.ToList().AsReadOnly();
}
}
public virtual void AddWall(Wall wall)
{
wall.Store = this;
_walls.Add(wall);
}
}
}
Get Action:
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult EditStore(Guid Id)
{
Store store;
using (UnitOfWork.Start())
{
store = _storeRepository.GetStore(Id);
}
return View(store);
}
Post Action (yes I realize there are no encoding checks etc, just rough here at first):
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditStore(Store store)
{
using (UnitOfWork.Start())
{
_storeRepository.Update(store);
UnitOfWork.Current.Flush();
}
return RedirectToAction("EditStore", store.Id);
}
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Address1">Address1:</label>
<%= Html.TextBox("Address1", Model.Address1) %>
<%= Html.ValidationMessage("Address1", "*") %>
</p>
<p>
<label for="Address2">Address2:</label>
<%= Html.TextBox("Address2", Model.Address2) %>
<%= Html.ValidationMessage("Address2", "*") %>
</p>
<p>
<label for="City">City:</label>
<%= Html.TextBox("City", Model.City) %>
<%= Html.ValidationMessage("City", "*") %>
</p>
<p>
<label for="County">County:</label>
<%= Html.TextBox("County", Model.County) %>
<%= Html.ValidationMessage("County", "*") %>
</p>
<p>
<label for="OLDStoreNumber">OLDStoreNumber:</label>
<%= Html.TextBox("OLDStoreNumber", Model.OLDStoreNumber) %>
<%= Html.ValidationMessage("OLDStoreNumber", "*") %>
</p>
<p>
<label for="FaxNumber">FaxNumber:</label>
<%= Html.TextBox("FaxNumber", Model.FaxNumber) %>
<%= Html.ValidationMessage("FaxNumber", "*") %>
</p>
<p>
<label for="HasHydraulicHose">HasHydraulicHose:</label>
<%= Html.TextBox("HasHydraulicHose", Model.HasHydraulicHose) %>
<%= Html.ValidationMessage("HasHydraulicHose", "*") %>
</p>
<p>
<label for="HasInsCounter">HasInsCounter:</label>
<%= Html.TextBox("HasInsCounter", Model.HasInsCounter) %>
<%= Html.ValidationMessage("HasInsCounter", "*") %>
</p>
<p>
<label for="HasPaintBooth">HasPaintBooth:</label>
<%= Html.TextBox("HasPaintBooth", Model.HasPaintBooth) %>
<%= Html.ValidationMessage("HasPaintBooth", "*") %>
</p>
<p>
<label for="IsHubStore">IsHubStore:</label>
<%= Html.TextBox("IsHubStore", Model.IsHubStore) %>
<%= Html.ValidationMessage("IsHubStore", "*") %>
</p>
<p>
<label for="IsOpenNightsWeekends">IsOpenNightsWeekends:</label>
<%= Html.TextBox("IsOpenNightsWeekends", Model.IsOpenNightsWeekends) %>
<%= Html.ValidationMessage("IsOpenNightsWeekends", "*") %>
</p>
<p>
<label for="NewStoreNumber">NewStoreNumber:</label>
<%= Html.TextBox("NewStoreNumber", Model.NewStoreNumber) %>
<%= Html.ValidationMessage("NewStoreNumber", "*") %>
</p>
<p>
<label for="PhoneNumber">PhoneNumber:</label>
<%= Html.TextBox("PhoneNumber", Model.PhoneNumber) %>
<%= Html.ValidationMessage("PhoneNumber", "*") %>
</p>
<p>
<label for="PostalCode">PostalCode:</label>
<%= Html.TextBox("PostalCode", Model.PostalCode) %>
<%= Html.ValidationMessage("PostalCode", "*") %>
</p>
<p>
<label for="State">State:</label>
<%= Html.TextBox("State", Model.State) %>
<%= Html.ValidationMessage("State", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
Fiddler:
Address1=1234+Main+street&Address2=Suite+100&City=Anywhere&County=MyCounty&OLDStoreNumber=1&FaxNumber=18001112222&HasHydraulicHose=true&HasHydraulicHose=false&HasInsCounter=true&HasInsCounter=false&HasPaintBooth=false&IsHubStore=false&IsOpenNightsWeekends=false&NewStoreNumber=1&PhoneNumber=18001112222&PostalCode=11001&State=MyState
The problem was with the IoC framework I was using. Well it wasn’t a problem with the framework itself, but rather my configuration of it. When using Castle Windsor for your IoC framework you must mark the containers being registered as having a lifestyle = Transient instead of the default Singleton. If you allow the default it will wreak havoc when using it as I am.
Thank you all for your help.