1

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" />
}
2
  • have you tried to set the action you want to post? like this: @using(Html.BeginForm("AddIngridient","YourControllerName")) Commented Mar 16, 2016 at 17:06
  • @RicardoPontual this is only needed when you want to jump to other controller outside the current route. And, absolutely, not represent a problem in the context of this question. Commented Mar 16, 2016 at 17:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If ViewBag.ID = id, contain value that need to place in model.IngridientID, maybe it should be something like @Html.HiddenFor(model => ViewBag.ID)
@A191919 no, it' better to set it to ingridientid model property in controller. That's mvc way
@A191919 The correct helper is: @Html.Hidden("IngedientID", ViewBag.ID). But my answer explains the right way to work with models, considering that you can add more properties to your model, and it is a bad practice to populate ViewBag with properties of the model.
1

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" />
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.