我目前正在MVC的一个网站上工作,而且我遇到了一些问题
我想要实现的目标:
当在DropDownList中选择一个项目时,它会使用form.Submit并转到RecipeController的帖子“AddIngredient”,在这个HttpPost中我添加逻辑以将该成分插入到SelectedIngredients列表中。然后,此列表将用于绑定到ListboxFor。
我遇到了什么问题:
当点击DropDownListFor中的项目时,它成功提交表单并转到HttpPost,问题是在将帖子转到/ Recipe / AddIngredient /并且没有留在提交视图
之后我的代码:
Submit.cshtml:
<div class="submit-recipe-form">
<div class="add-ingredient">
@Html.Partial("_AddIngredient");
</div>
_AddIngredient.cshtml(部分视图):
@model Recepten.Models.IngredientSelectModel
@using (Ajax.BeginForm("AddIngredient", "Recipe", new AjaxOptions() { UpdateTargetId = "add-ingredient", HttpMethod = "Post" }))
{
@Html.LabelFor(model => model.Ingredients, "Add Ingredient")
@Html.DropDownListFor(model => model.SelectedIngredient, Model.Ingredients, new { @onchange = "this.form.submit();" })
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
RecipeController:
[HttpPost]
public ActionResult AddIngredient(IngredientSelectModel ing)
{
//Logic
return PartialView(ing);
}
IngredientSelectModel:
public class IngredientSelectModel
{
public RecipeModel Recipe { get; set; }
public IEnumerable<SelectListItem> Ingredients { get; set; }
public int SelectedIngredient { get; set; }
public string addIngredient { get; set; }
public List<int> SelectedIngredients { get; set; }
public IngredientSelectModel()
{
SelectedIngredients = new List<int>();
}
}
我已经安装了包jquery unobtrusive ajax并且在我的包中正确列出了,我希望我在HttpPost中缺少一些视图,但不确定究竟是什么。
感谢您的时间!
编辑:
Submit.cshtml http://pastebin.com/yS8etDK5
答案 0 :(得分:0)
我可以将@onchange = "$(this).parents('form').submit();"
替换为AVPlayerLayer