A191919
A191919

Reputation: 3442

ASP.NET MVC pass parameter to controller with argument

I have problem with passing parameter in controller.

I get id value in public ActionResult AddIngridient(int id = 0) and i need to pass it in public ActionResult AddIngridient(Ingredients ingridients) like IngredientID. How to do this?

I have model:

public partial class Ingredients
{
    public int IngredientID { get; set; }
    public Nullable<int> AmountID { get; set; }
    public Nullable<int> IngredientTypeID { get; set; }
}

And controllers:

public ActionResult AddIngridient(int id = 0)
    {
        IEnumerable<Ingredient> ListOfIngridient =  FRE.Ingredient.Select(key => key).ToList();
        ViewBag.IngridientsList = new SelectList(ListOfIngridient,"IngredientID", "IngredientName");

        IEnumerable<Amount> ListOfAmounts = FRE.Amount.Select(key => key).ToList();
        ViewBag.AmountsList = new SelectList(ListOfAmounts, "AmountID", "AmountName");

        ViewBag.ID = id;
        return View();
    }
    [HttpPost]
    public ActionResult AddIngridient(Ingredients ingridients)
    {
        return View();
    }

View looks like:

@model FoodRecipes.Models.Ingredients
@{
ViewBag.Title = "AddIngridient";
}

<h2>AddIngridient</h2>
@using(Html.BeginForm())
{ 
@Html.DropDownListFor(model => model.IngredientTypeID, (SelectList)ViewBag.IngridientsList)
<br />
@Html.DropDownListFor(model => model.AmountID, (SelectList)ViewBag.AmountsList)
 <input type="submit" value="Create" />
}

Upvotes: 1

Views: 54

Answers (2)

Balde
Balde

Reputation: 590

You should change your code of AddIngredient Action to return the model:

public ActionResult AddIngridient(int id = 0)
{
    IEnumerable<Ingredient> ListOfIngridient =  FRE.Ingredient.Select(key => key).ToList();
    ViewBag.IngridientsList = new SelectList(ListOfIngridient,"IngredientID", "IngredientName");

    IEnumerable<Amount> ListOfAmounts = FRE.Amount.Select(key => key).ToList();
    ViewBag.AmountsList = new SelectList(ListOfAmounts, "AmountID", "AmountName");

    return View(new Ingredients { IngredientID = id });
}

And in your view add a input type hidden:

@using(Html.BeginForm())
{ 
   @Html.HiddenFor(model => model.IngredientID)
   @Html.DropDownListFor(model => model.IngredientTypeID, (SelectList)ViewBag.IngridientsList)
   <br />
   @Html.DropDownListFor(model => model.AmountID, (SelectList)ViewBag.AmountsList)
   <input type="submit" value="Create" />
}

Upvotes: 1

teo van kot
teo van kot

Reputation: 12491

You should use @Html.HiddenFor() helper.

In your case:

@using(Html.BeginForm("AddIngridient","YourControllerName"))
{ 
   @Html.HiddenFor(x => x.IngredientID) //this line 
   @Html.DropDownListFor(model => model.IngredientTypeID, (SelectList)ViewBag.IngridientsList)
   <br />
   @Html.DropDownListFor(model => model.AmountID, (SelectList)ViewBag.AmountsList)
   <input type="submit" value="Create" />
}

It will produce input type hidden with name IngredientID that will be passed to server on form POST.

Upvotes: 1

Related Questions