views:

975

answers:

2

I need to do some calculations on an ASP.NET MVC View, an action different than the form submission. I've tried various methods of passing the current Model on to a new controller action via an ActionLink, but the model doesn't appear to be passed.

public ActionResult Calculate(MuralProject proj)
{
    ProjectFormRepository db = new ProjectFormRepository();
    List<Constant> constants = db.GetConstantsByFormType(FormTypeEnum.Murals);

    proj.Materials = new MuralMaterials();
    proj.Materials.Volunteers = this.GetVolunteerCount(constants, proj);

    this.InitializeView(); 
    return View("View", proj);
}

What would my Html.ActionLink syntax need to be in order for me to call this and have the returning view have the same model data (with the calculated changes)? Alternately, is there another way to accomplish this?

I also tried an Ajax.ActionLink method but I ran into the same problem

Edit: "Give your submit buttons a name, and then inspect the submitted value in your controller method" method shown here is what I was looking for.

A: 

An action link just links to an action. It translates to a <a href="action">action</a> tag. The action it links to has no idea about the state of the page it has just left.

You should probably be 'POST'ing to an action, but it's only going to send form data, not an object (although mvc can automatically map Form fields to an object).

DaRKoN_
Ok, that may be helpful. I can't seem to figure out how to get the form data to be mapped to an object if it's not form submission that I'm doing.
paulwhit
How are you manipulating the data?
DaRKoN_
i have a set of fields that combine with some server-based calculations to populate another set of fields. all fields are in the Model
paulwhit
So post the modified fields to the new action, and also post an identifier for the model to be updated. In your action, load the model as per the identifier, and then replace with the modified values?
DaRKoN_
+4  A: 

[Saw your comments; I'll repost this answer here so you can mark the question resolved, and mark it community wiki so I don't get rep for it - Dylan]

Give your submit buttons a name, and then inspect the submitted value in your controller method:

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>

posting to

public class MyController : Controller {
    public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Send":
                // delegate sending to another controller action
                return(Send());
            case "Cancel":
                // call another action to perform the cancellation
                return(Cancel());
            default:
                // If they've submitted the form without a submitButton, 
                // just return the view again.
                return(View());
        }
    }

    private ActionResult Cancel() {
        // process the cancellation request here.
        return(View("Cancelled"));
    }

    private ActionResult Send() {
        // perform the actual send operation here.
        return(View("SendConfirmed"));
    }

}
Dylan Beattie
this is depressing if we have to do things like this, whatever happened to 'separation of concerns' argument? microsoft hoo haa marketing rhetoric! i'm depressed that this is the best a framework can provide for such a common scenario
Erx_VB.NExT.Coder
What concerns do you feel are not being clearly separated? If the magic strings bother you, it's easy to set up a static class or external resource file containing the button captions and values so you're not relying on magic-string comparisons.
Dylan Beattie