0

I'm new to MVC and still figuring out how everything works. Currently, I am trying to call a method that is in my controller from my view but I'm not sure the best way to go about this. Here is my SalesOrdersController method that I want to run.

public ActionResult revertToPending(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    SalesOrder salesOrder = db.SalesOrders.Find(id);

    if (salesOrder == null)
    {
        return HttpNotFound();
    }

    ViewBag.statusList = new List<Object>
        {
            new {value = SOStatus.Pending, text = "Pending" },
            new {value = SOStatus.Released, text = "Released" },
            new {value = SOStatus.Released, text = "Shipped" },
            new {value = SOStatus.BackOrdered, text = "Back Ordered" },
            new {value = SOStatus.Cancelled, text = "Cancelled" },

        };

    return View(salesOrder);
}

Here is my view


    <div class="form-group">
        @Html.LabelFor(model => model.SalesOrderStatus, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.SalesOrderStatus, new SelectList(ViewBag.statusList, "value", "text"), new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.SalesOrderStatus, "", new { @class = "text-danger" })          

            @Html.ActionLink("Revert to Pending", "revertToPending", "SalesOrder")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.BillingStatus, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EnumDropDownListFor(model => model.BillingStatus, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.BillingStatus, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Details, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextAreaFor(model => model.Details, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Details, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn" />
        </div>
    </div>

And I want that to run when the user presses a button from the view.

So something like this

@Html.ActionLink("Revert to Pending", "revertToPending", "SalesOrder" )

I know that I'm doing it wrong as I don't want it to lead to the revertToPending View as it doesn't exist. But I want that controller method to run when the link is pressed.

Thanks

chris
  • 121
  • 11
  • Where's the relevant HTML code? Is there a form? Personally if you are just passing a simply value such as an int or a string to a controller method, I would use an ajax call. However, if you want to use an `ActionLink`, you can look at this example https://stackoverflow.com/questions/8803928/how-to-send-data-through-actionlink-in-asp-net – JamesS Feb 06 '20 at 15:36
  • @JamesS Yeah I'm just brand new to this so I just don't think I properly understand how everything works right now and don't know the best way to do things. Because I want the user to be able to click the "Revert to pending" link and then have the controller render the view to the pending page. – chris Feb 06 '20 at 15:48
  • 1
    What do you mean by 'render the view to the pending page'? Do you mean take the user to a new page or show a partial view on the current page? – JamesS Feb 06 '20 at 15:52
  • @JamesS I added my relevant html to help you understand but it would just be a partial view on the same page. Because the Page slightly differs when the "SOStatus = pending" compared to when the "So status = released" so I just want it to revert back to how it didlook when it was originally "released" when the link is pressed – chris Feb 06 '20 at 15:57
  • If you just want to 'revert back' to whatever it was before, you can just call the controller method that create/ shows the view with whatever (if any) parameters you initially sent it. This wouldn't really be a partial view as you want to, by the sounds of it, reload the page – JamesS Feb 06 '20 at 15:59
  • @JamesS Hmm yeah I thought I could just put a button on the page that would run the "revertToPending" method. – chris Feb 06 '20 at 16:05
  • if you don't want the view to change then use ajax to do the work then handle the server's response in the success/error functions of the ajax request. you might be able to return null on your controller action as you have it to get the behavior you want but you wont be able to take action on the client side according to if the action was successful or not – GregH Feb 06 '20 at 18:10

0 Answers0