I have a table with a GUID primary key into which I'm trying to insert a newly-created object using an ASP.NET MVC Controller's UpdateModel
method (db is a LINQ-to-SQL data context):
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
Fields field = new Fields();
field.ID = Guid.NewGuid();
try
{
UpdateModel(field);
db.Fields.InsertOnSubmit(field);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View("Edit", field);
}
}
First off, it seems odd that the auto-generated views for Edit and Create on the model would include a primary key field for the user to edit, but it's easy enough to delete those.
But I'm running into a problem where the call to UpdateModel
throws an exception when the primary key is not passed in as part of the form collection.
Am I missing some way of flagging the primary key column as something that the user (and by extension, the call to UpdateModel
) shouldn't be mucking with? I could add an includeProperties
array and leave the primary key's name out, but that's not very DRY).
I could also add a hidden field to my form incorporating the primary key -- with a GUID primary key I can do this on the GET request to Create.