Home > other >  How to pass a model and a variable to a post method with one input
How to pass a model and a variable to a post method with one input

Time:12-15

I'm trying to have one post route that takes care of multiple operations.

Here's the Controller:

[HttpPost("dish/{operation}")]
    public IActionResult Dish(string operation, Dish dish)
    {
        if (operation == "RedEdit")
        {
            return RedirectToAction("EditDish", dish);
        }
        if (ModelState.IsValid)
        {
            if (operation == "Add")
            {
                _context.Add(dish);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            else //Unused currently
            {
                Console.WriteLine("Oops");
                _context.Add(dish);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        else
        {
            return View("DishForm");
        }
    }

The POST route will take a string, which is the operation it'll do, and depending on the operation it'll run something different. Right now I don't have all the operations, and my else within the validation isn't what it's going to be. The problem I'm having currently is with the "RedEdit," which is just a method to redirect to the edit page. Here's my view and what I'd like to do:

@{
    ViewData["Title"] = "Add a dish!";
    ViewData["Header"] = "Add a new Dish!";
    ViewData["Home"] = true;
    ViewData["Add"] = false;
    var parms = new Dictionary<string, string>
{
{"operation", ""}
};
}
@model RichCRUDelicious.Models.Dish

<div >
    <h3><u>@Model.Name by @Model.Chef</u></h3>
    <p>@Model.Description</p>
    <p>Calories: @Model.Calories</p>
    <p>Tastiness: @Model.Tastiness</p>
    <footer>
        @using (Html.BeginForm("Dish", "Home", "operation", FormMethod.Post)){
            //Two Buttons with Edit and Delete
        }
    </footer>
</div>

I'd essentially like to have one form, which has two buttons, one for edit and delete. The button for edit will change my operation value in parms to "RedEdit," while delete will change it to "Delete" (which I don't have a route set up for currently but that's not the issue.) I've tried a couple different methods, and mostly the issue comes down to the parameters within the post method, I'm not sure how I can pass the model in AND the operation value. I don't mind if they're split up into two different forms, but I'd really like just one post method for this controller.

I've tried using a generic HTML Form with:

<form asp-action="Dish" asp-controller="Home" asp-all-route-data="parms" method="post">

But my issue wasn't resolved using this method either, I'm thinking a hidden input with two different forms will work, but if there's a better way I'd like to hear.

CodePudding user response:

If you want to use a form with two buttons which go to the same action,you can try to add asp-route-operation to your buttons,here is a simple demo:

Dish:

public class Dish {
        public int Id { get; set; }
        public string Name { get; set; }

    }

view:

<form method="post">
    <input hidden name="Id" value="1" />
    <input hidden name="Name" value="test" />
    <button asp-controller="Home" asp-action="Dish" asp-route-operation="RedEdit">RedEdit</button>
    <button asp-controller="Home" asp-action="Dish" asp-route-operation="Delete">Delete</button>

</form>

action:

[HttpPost("dish/{operation}")]
        public IActionResult Dish(string operation, Dish dish)
        {
            ...
        }
    }

Hidden inputs will help you pass the value of Dish to the action,asp-route-operation will help pass different operation values to the action.When clicking RedEdit button,the value of operation will be RedEdit.With Delete button,it will be Delete.

  • Related