我已经尝试了几个星期来关注如何创建动态表单的几个教程,使其能够在表单中添加另一个"成分".这是我试图遵循的文章.http://www.joe-stevens.com/2011/07/24/asp-net-mvc-2-client-side-validation-for-dynamic-fields-added-with-ajax/
现在我正在使用添加链接添加多个recipeIngredients,但是我需要同时在单击链接时添加"ingredientName"和"recipeIngredient"数量.我的问题是,当我运行应用程序时,recipeingredient的表单有一个0而不是一个实际的文本框.当我点击添加新成分时,我可以添加一个文本框,但是当我输入金额并单击保存时,模型数据不会传递给控制器.
我甚至不知道从哪里开始修复这个,我不确定我是否应该使用viewmodel或者我是否会完全错误.这是我的数据库图表http://i44.tinypic.com/xp1tog.jpg.
这是我的CreateView:
@model ViewModels.RecipeViewModel
@using Helpers;
<h2>CreateFullRecipe</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function () {
$("#add-recipeingredient").click(function () {
$.ajax({
url: '@Url.Action("GetNewRecipeIngredient")',
success: function (data) {
$(".new-recipeingredients").append(data);
Sys.Mvc.FormContext._Application_Load();
}
});
});
});
</script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Recipe</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Recipe.RecipeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Recipe.RecipeName)
@Html.ValidationMessageFor(model => model.Recipe.RecipeName)
</div>
</fieldset>
<fieldset>
<legend>RecipeIngredients</legend>
<div class="new-recipeingredients">
@Html.EditorFor(model => model.RecipeIngredients)
</div>
<div style="padding: 10px 0px 10px 0px">
<a id="add-recipeingredient" href="javascript:void(0);">Add another</a> …Run Code Online (Sandbox Code Playgroud)