0

Controller

    [ActionName("Index")]
    [HttpPost]
    public ActionResult IndexPost(string button, int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        string buttonClicked = Request.Form["SubmitButton"];
        if(buttonClicked == "Accept")
        {
            CurrentApplication currentApplication = db.CurrentApplications.Find(id);
            currentApplication.AppStatus = "APPROVED";
            db.SaveChanges();

        }
        else if(buttonClicked == "Decline")
        {
            CurrentApplication currentApplication = db.CurrentApplications.Find(id);
            currentApplication.AppStatus = "UNAPPROVED";
            db.SaveChanges();

        }
        //Save Record and Redirect
        return RedirectToAction("Index");
    }

Index View

<button type="submit" name="SubmitButton" value="Approve" class="btn btn-sm btn-success">Approve</button>
<button type="submit" name="SubmitButton" value="Unapprove" class="btn btn-sm btn-danger">Unapprove</button>

In my Index view, i have a table where there are rows of data i can "Approve" or "Disapprove" using 2 buttons. I tried using these http://www.scriptscoop.net/t/b7bd27aee268/c-asp-net-mvc-two-different-buttons-in-form-submit.html and ASP.NET MVC two different buttons in form submit. I want the status value to change to Approve or Disapprove when the user clicks the corresponding button. But I'm not sure why it isn't working as I tried to code it similarly to the Edit view.

Community
  • 1
  • 1

3 Answers3

3

You can get the value of the submit button as a parameter to your Action method, now all you have to do is compare its value inside the Action and perform the changes you need . In your view the value of the buttons are value="Approve" for the Approve button and value="Unapprove" for the Unapprove button whereas you are comparing it with "Accept" and "Decline".

[ActionName("Index")]
    [HttpPost]
    public ActionResult IndexPost(string SubmitButton, int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        string buttonClicked = SubmitButton;
        if(buttonClicked == "Approve")
        {
            CurrentApplication currentApplication = db.CurrentApplications.Find(id);
            currentApplication.AppStatus = "APPROVED";
            db.SaveChanges();

        }
        else if(buttonClicked == "Unapprove")
        {
            CurrentApplication currentApplication = db.CurrentApplications.Find(id);
            currentApplication.AppStatus = "UNAPPROVED";
            db.SaveChanges();

        }
        //Save Record and Redirect
        return RedirectToAction("Index");
    }
Vibhesh Kaul
  • 2,593
  • 1
  • 21
  • 38
0

In your HTML, the values for Approve and Unapprove buttons are Approve and Unapprove respectively. However, in your code, you are comparing buttonClicked with Accept and Decline.

It should be like this:

[ActionName("Index")]
[HttpPost]
public ActionResult IndexPost(string button, int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    string buttonClicked = Request.Form["SubmitButton"];
    if(buttonClicked == "Approve") // value of Approve button
    {
        CurrentApplication currentApplication = db.CurrentApplications.Find(id);
        currentApplication.AppStatus = "APPROVED";
        db.SaveChanges();

    }
    else if(buttonClicked == "Unapprove") // value of Unapprove button
    {
        CurrentApplication currentApplication = db.CurrentApplications.Find(id);
        currentApplication.AppStatus = "UNAPPROVED";
        db.SaveChanges();

    }
    //Save Record and Redirect
    return RedirectToAction("Index");
}
Senjuti Mahapatra
  • 2,570
  • 4
  • 27
  • 38
  • Oh I see thank you. Is there anything wrong with my code logic as well? Because it seems right to me but it doesn't work. Thank you! :> – Cucumber Ninja Dec 24 '15 at 07:10
0

you can try this ;

Controller:

    [ActionName("Index")]
    [HttpPost]
    public ActionResult IndexPost(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var result = Request.Form["result"];
        CurrentApplication currentApplication = db.CurrentApplications.Find(id);
        currentApplication.AppStatus = result;
        db.SaveChanges();


        //Save Record and Redirect
        return RedirectToAction("Index");
    }

View

<input type="hidden" name="result" id="result" />
<a  data-value="Approve" class="btn btn-sm btn-success submitButton">Approve</a>
<a  data-value="Unapprove" class="btn btn-sm btn-danger submitButton">Unapprove</a>

Javascript

<script>
   $('.submitButton').on('click', function (e) {
    e.preventDefault();

    $('#result').val($(this).data('value'));

    $('form').submit();
   });
</script>
Zergling
  • 526
  • 2
  • 7
  • @zengling hi sorry its a little late but it constantly logs me out cause of the javascript part. I'm not familiar with javascript and have no idea how to do it. Mind helping me out once more? Thank you :) – Cucumber Ninja Dec 28 '15 at 06:31