Hi There,
(My first question, so hello all!) I am using C# and the ASP.NET MVC Framework Beta 1 and was wondering if this was the most efficient way to achieve two, form posting scenarios using a single partial and also how to get the edit mode working. The partial contains my html form code so it can be recycled in both an add and edit scenario.
Now I will state this code is working well for "add", but "edit" mode will not work and you will see why. Also if there is a more syntaxically or orthodox approach to the two-form-single-partial hook up I would be interested to hear.
My partial is _Save.aspx, relevant code:
<%using (Html.BeginForm(c => c.Save(null), FormMethod.Post, new {@class = "form"})) .....
The two partial "host" pages, for lack of a better word are Add.aspx and Edit.aspx and fire up the _Save.aspx partial.
My controller; ModuleController, relevant code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save([Bind(Include = "Name,Description,Status")] Module module)
{
try
{
moduleService.SaveModule(module);
TempData["Message"] = "Record updated.";
return RedirectToAction("Index");
}
catch (Exception ex)
{
TempData["Message"] = "An internal error occured - " + ex.ToString();
return View(module);
}
}
1) My first question lies with c.Save(null) featured in the BeginForm() tag.
At the point of declaration, I do not have a Module to pass in to Save() (I am taking advantage of "Model Binders" here), so that's why I am passing null. However is this the best approach? The parameter list is just a bit ugly. Thankfully at runtime this object is re-populated, so the 'functionality' works as intended.
2) I cannot edit - as obviously I've explicitly defined the Save() method in the BeginForm() and it does not include the id parameter, therefore I do not receive it in my Save() action. If I add id as a nullable int to my Save() action, and state null on my Save() method on the BeginForm(), then it is always null. Really need help with this one :)
All help welcome!
PS: Please do not judge my error handling strategy, it is only temporary :)