我有一个典型的Create Action,它是由MVC 4中的脚手架生成的。没什么特别的。
通常我通过以下URL /RecipeLines/Create
到达此页面,当我创建该特定记录时,我有一个下拉列表,如果已发送,我已更改为接收recipeID:
<div class="editor-label">
@Html.LabelFor(model => model.RecipeID, "Recipe")
</div>
<div class="editor-field">
@Html.DropDownList("RecipeID", (SelectList)ViewBag.RecipeID, string.Empty)
@Html.ValidationMessageFor(model => model.RecipeID)
</div>
我想要做的是通过传递一个如下所示的recipeID链接到“创建”页面:
/RecipeLines/Create/2
我已编辑@Html.DropDownList
以接受参数,HOPEFULLY选择正确的RecipeID,或者如果链接到没有查询字符串
/RecipeLines/Create
然后它没有选定的值。
这是我的创建行动。
// GET: /RecipeLines/Create
public ActionResult Create(int? recipeID)
{
ViewBag.MeasurementID = new SelectList(db.Measurements, "MeasurementID", "MeasurementEn");
ViewBag.RecipeID = new SelectList(db.Recipes, "RecipeID", "RecipeNameEn", recipeID);
ViewBag.IngredientID = new SelectList(db.Ingredients, "IngredientID", "IngredientNameEn");
return View();
}
My 2 @ Html.ActionLink(s)如下所示:
//Coming from the Recipe Page
@Html.ActionLink("+", "Create", "RecipeLines", new { recipeID = recipe.RecipeID }, null);
//Coming from the RecipeLines/Index page
@Html.ActionLink("Create New", "Create")
THIS PROBLEM HAS BEEN SOLVED AND THE ABOVE CODE NOW WORKS
答案 0 :(得分:1)
尝试以下代码:
制作行动
public ActionResult Create(int? recipeId)
{
ViewBag.RecipeID = new SelectList(db.Recipes, "RecipeID", "RecipeNameEn",recipeId);
//
return View();
}
查看强>
@Html.DropDownList("RecipeID", (SelectList)ViewBag.RecipeID,string.Empty)
<强> ActionLink的强>
@Html.ActionLink("+", "Create", "RecipeLines", new { recipeId= recipe.RecipeID }, null);
@Html.ActionLink("Create New", "Create")