For some reason, the edit action below get to the edit view form fine, but when I hit submit on my view page to post on the second method below, the companyToEdit structure do not contain the companyID and I get an exception. This is weird since, I have done this before and I get the id field in other instances.
If I put in the view the section of code below:
<p>
<label for="CompanyID">CompanyID:</label>
<%= Html.TextBox("CompanyID", Model.CompanyID) %>
<%= Html.ValidationMessage("CompanyID", "*") %>
</p>
Then it will work just fine. But if I take that away, since the CompanyID is a read-only field it stops working. Any ideas?
//
// GET: /Company/Edit/5
public ActionResult Edit(int id)
{
var companyToEdit = _repository.GetCompany(id);
return View(companyToEdit);
}
//
// POST: /Company/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Company companyToEdit)
{
if (!ModelState.IsValid)
return View();
try
{
_repository.Update(companyToEdit);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
EDIT 2: I guess I can do something similar to this, and hide the ID in a hash value like the one below. I think this will be the ultimate solution, where we will get the most secure and the less complexity.
<input id="fkey" name="fkey" type="hidden" value="4a3d337bf38b35e9ff4167cfa878160b">