0
votes

I would like to have two buttons in my view (called Create), one that submits the form and takes the user back to home page if they are finished and one that submits the form but reloads the rating page to be able to add additional ratings.

Here is the problem that I have right now-

Currently I have one button that has an action result in my controller:

public ActionResult Create(Rating rating) 
{
        if (ModelState.IsValid)
        {
            db.Ratings.Add(rating);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
    **Additional code that is irrelevant here
}

The problem that I am faced with is that this ActionResult has a Redirect in it to the homepage so when I submit my other button and use this same ActionResult class it is being redirected to the homepage. I am using the javascript onclick event in the view to redirect to the Ratings page when the button is clicked and the form is submitted but if I use this same Action Result class for both buttons it redirects the button I want to keep me on the page to the index page.

How do I create two Action Result classes from the same view, one for each submit button?

3

3 Answers

2
votes

Well, how do you determine what the user wants to do?

Both buttons submit the form, so they may as well still use the same action. But you need to differentiate somehow. You can do that with the buttons.

Let's say you have these two buttons:

<input type="submit" name="redirect" value="true" />
<input type="submit" name="redirect" value="false" />

Then you can bind that in your action method:

public ActionResult Create(Rating rating, bool redirect)
{
    // other logic

    if (redirect)
        return RedirectToAction("Index");
    else
        return View(rating);
}

If you are ever going to have more than two possible options then you might use a string instead of a boolean. Something like:

<input type="submit" name="action" value="redirect" />
<input type="submit" name="action" value="reload" />

And then in the controller:

public ActionResult Create(Rating rating, string action)
{
    // other logic

    if (action.Equals("redirect"))
        return RedirectToAction("Index");
    else if (action.Equals("reload"))
        return View(rating);
    else if //...
        //... and so on
}

The point is that the client-side code needs to tell the server-side code what to do somehow. Including that on the form submission itself makes the form submission self-describing and allows the server-side code to handle it easily.

0
votes

Example of how it use

Html, inside form:

<button type="submit" name="TaskSubmitAction" value="ActionReject" class="btn btn-danger pull-left">Reject</button>
<button type="submit" name="TaskSubmitAction" value="ActionSubmit" class="btn btn-success">Accept</button>

Controller:

    public ActionResult TaskSubmit(int? id, string TaskSubmitAction)
    {
        switch (TaskSubmitAction)
        {
            case "ActionSubmit":

                break;
            case "ActionReject":

                break;
            default: throw new Exception();
        }
0
votes

In your html give both buttons the same 'name' attribute but assign two different values.

<button name="submitBtn" value="valueX"> Button 1 </button>
<button name="submitBtn" value="valueY"> Button 2 </button>

In your server side code get the value of the input button and based on this value carry out different actions

String choice = request.getParamter("submitBtn");
if(choice.equals("valueX"))
    //do something
else if(choice.equals("valueY"))
    //do something else