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