views:

489

answers:

4

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 :)

A: 

Great question... great work on getting into MVC.

1) If you don't like doing "Save(null)", you can use an overload where you specify the Action by name, as opposed to doing the Func way. But I think what you're doing is fine.

2) As for the second 'edit' part... I have to ask, is there an "ID" on the "Module" class? If so, add "ID" to your Include, and then check if it's 0 (or NULL or whatever). You can use a hidden input field to specify id=0 for new, and id=blah for edits.

Timothy Khouri
A: 

Thanks tim!

Works a treat. I do have an ID in my Module class and it would fit in as the id parameter just perfectly of course! Didn't think of that.

I did two code updates:

<% using (Html.BeginForm(c => c.Save(ViewData.Model.Module.ID, null), FormMethod.Post, new {@class = "form"}))...

and:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(int? id, [Bind(Include = "Name,Description,Status")] Module module)
        {
            try
            {
                module.ID = id.HasValue ? id.Value : 0;

Thanks again Tim!

Any other contributions from others are still welcome!

GONeale
I'm glad it worked out!
Timothy Khouri
A: 

I still wish to know if this is the most efficient approach, any MVC experts, please do not hesitate to reply :)

GONeale
A: 

Did you create a binder for the [Bind] ?

When I create a simple sample with Bind, it is 'null'

I'm using string for the BeginForm as well




[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ComplexFormSample([Bind]Person person)