Hi:
I am using MVC and have created a strongly type model to post data to a page.
The data combines and address and company information. I set the address ID and Company ID in the model information and send it to the view. The view may or may not display the ID's. When the form is submited the ID values the are not valid.
get and display the information
public ActionResult AddressDetail(int id, string cName, int addressID)
{
var address = (from c in svc.Addresses where (c.AddressID == addressID) select c).First();
Models.AddressEdit addressEdit = new AddressEdit();
addressEdit.CompanyID = id;
addressEdit.CompanyName = cName;
addressEdit.AddressID = address.AddressID;
addressEdit.Street = address.Street1;
addressEdit.Street1 = address.Street2;
addressEdit.City = address.City;
addressEdit.State = address.State;
addressEdit.ZipCode = address.ZipCode;
addressEdit.Zip4 = address.Zip4;
return View(addressEdit);
}
after the form has been sumbited back to the controller
[HttpPost]
public ActionResult AddressDetail( Models.AddressEdit addressToEdit) //FormCollection form)
{
var address = (from c in svc.Addresses where (c.AddressID == addressToEdit.AddressID) select c).First();
address.Street1 = addressToEdit.Street;
address.Street2 = addressToEdit.Street1;
address.City = addressToEdit.City;
address.ZipCode = addressToEdit.ZipCode;
address.Zip4 = addressToEdit.Zip4;
svc.UpdateObject(address);
svc.SaveChanges();
return RedirectToAction("Details");
}
below is the address view <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Address
<% using (Ajax.BeginForm("AddressDetail", "Company", new AjaxOptions { UpdateTargetId = "TextArea" } )) { %> Edit Address
<div class="display-label">CompanyName: <%= Html.Encode(Model.CompanyName) %></div>
<div class="display-label">Address ID: <%= Html.TextBoxFor(model => model.AddressID) %></div>
<%= Html.LabelFor(model => model.Street) %>
<%= Html.TextBoxFor(model => model.Street) %>
<%= Html.ValidationMessageFor(model => model.Street) %>
<%= Html.LabelFor(model => model.Street1) %>
<%= Html.TextBoxFor(model => model.Street1) %>
<%= Html.ValidationMessageFor(model => model.Street1) %>
<%= Html.LabelFor(model => model.City) %>
<%= Html.TextBoxFor(model => model.City) %>
<%= Html.ValidationMessageFor(model => model.City) %>
<%= Html.LabelFor(model => model.State) %>
<%= Html.TextBoxFor(model => model.State) %>
<%= Html.ValidationMessageFor(model => model.State) %>
<%= Html.LabelFor(model => model.ZipCode) %>
<%= Html.TextBoxFor(model => model.ZipCode)%>
<%= Html.ValidationMessageFor(model => model.ZipCode)%>
<%= Html.LabelFor(model => model.Zip4) %>
<%= Html.TextBoxFor(model => model.Zip4)%>
<%= Html.ValidationMessageFor(model => model.Zip4)%>
</fieldset>
<p>
<%=Html.ActionLink("Return Company Details", "Index") %>
</p>
<input type="submit" value="AddressDetail" />
</div>
<% } %>