私は数週間、フォームに別の「成分」を追加する能力を与える動的フォームを作成する方法について、いくつかのチュートリアルに従っています。ここで私が追いつこうとした記事があります。 http://www.joe-stevens.com/2011/07/24/asp-net-mvc-2-client-side-validation-for-dynamic-fields-added-with-ajax/MVC 3 ViewModelを使用した動的フォーム
今すぐ追加リンクを使用して複数のrecipeIngredientsを追加しようとしていますが、リンクをクリックしたときに追加できる「ingredientName」と「recipeIngredient」Amountの両方が必要です。 私の問題は、私がアプリケーションを実行すると、recipeingredientのフォームには実際のテキストボックスの代わりに0があるということです。新しい成分の追加をクリックすると、追加するテキストボックスが表示されますが、量を入力して保存をクリックすると、モデルデータはコントローラに渡されません。
これを修正することから始めて、私はビューモデルを使用すべきかどうか、まったく間違っているかどうかはわかりません。ここに私のデータベース図http://i44.tinypic.com/xp1tog.jpgがあります。 recipeingredientため
@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>
</div>
</fieldset>
<div>
<input type="submit" value="CreateFullRecipe" />
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
マイeditortemplateview:
@model Models.RecipeIngredient
@using Helpers;
@using (Html.BeginAjaxContentValidation("form0"))
{
using (Html.BeginCollectionItem("RecipeIngedients"))
{
<div style="padding: 5px 0px 5px 0px">
@Html.LabelFor(model => model.Amount)
@Html.EditorFor(model => model.Amount)
@Html.ValidationMessageFor(model => Model.Amount)
</div>
}
}
MYコントローラーに関する方法:
[HttpGet]
public ActionResult CreateFullRecipe()
{
var recipeViewModel = new RecipeViewModel();
return View(recipeViewModel);
}
//
// POST: /Recipe/Create
[HttpPost]
public ActionResult CreateFullRecipe(RecipeViewModel recipeViewModel)
{
if (ModelState.IsValid)
{
db.Recipes.Add(recipeViewModel.Recipe);
db.SaveChanges();
int recipeID = recipeViewModel.Recipe.RecipeID;
for (int n = 0; n < recipeViewModel.RecipeIngredients.Count(); n++)
{
db.Ingredients.Add(recipeViewModel.Ingredients[n]);
int ingredientID = recipeViewModel.Ingredients[n].IngredientID;
recipeViewModel.RecipeIngredients[n].RecipeID = recipeID;
recipeViewModel.RecipeIngredients[n].IngredientID = ingredientID;
db.RecipeIngredients.Add(recipeViewModel.RecipeIngredients[n]);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(recipeViewModel);
}
public ActionResult GetNewIngredient()
{
return PartialView("~/Views/Shared/IngredientEditorRow.cshtml", new Ingredient());
}
public ActionResult GetNewRecipeIngredient()
{
return PartialView("~/Views/Shared/_RecipeIngredientEditRow.cshtml", new RecipeIngredient());
}
ここ
は私CREATEVIEWです
マイビューモデル:
public class RecipeViewModel
{
public RecipeViewModel()
{
RecipeIngredients = new List<RecipeIngredient>() { new RecipeIngredient() };
Ingredients = new List<Ingredient>() { new Ingredient() };
Recipe = new Recipe();
}
public Recipe Recipe { get; set; }
public IList<Ingredient> Ingredients { get; set; }
public IList<RecipeIngredient> RecipeIngredients { get; set; }
}
}
私に知らせてください私の問題を助けるために必要なその他の情報がある場合。これは本当に私を夢中にしているので、私は何か助けを得ることを楽しみにしています ありがとう!
コントローラーポストメソッドcreatefullrecipeはあらかじめ定義されたリスト用で、ユーザーに別の要素を追加することを心配していないときに機能しました。ちょうど2つの成分私の見解には、これを作成するコードがコメントアウトされています。私が実際にやりたいことは、フォームモデルをコントローラに渡すビューモデルを取得し、createfullrecipeコントローラメソッドのようなデータを処理できることだけです。ここで
@* @for (int n = 0; n < Model.Ingredients.Count(); n++)
{
<div class="editor-label">
@Html.LabelFor(model => model.Ingredients[n].IngredientName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Ingredients[n].IngredientName)
@Html.ValidationMessageFor(model => model.Ingredients[n].IngredientName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.RecipeIngredients[n].Amount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.RecipeIngredients[n].Amount)
@Html.ValidationMessageFor(model => model.RecipeIngredients[n].Amount)
</div>
}*@
私のモデルクラスです:
public class Recipe
{
public int RecipeID { get; set; }
public string RecipeName { get; set; }
public string Description { get; set; }
public int? PrepTime { get; set; }
public int? CookTime { get; set; }
public string ImageURL { get; set; }
public virtual IList<RecipeTag> RecipeTags { get; set; }
public virtual IList<Rating> Ratings { get; set; }
public virtual IList<RecipeStep> RecipeSteps { get; set; }
public virtual IList<RecipeIngredient> RecipeIngredients { get; set; }
}
public class RecipeIngredient
{
public int RecipeIngredientID { get; set; }
public string IngredientDesc { get; set; }
public string Amount { get; set; }
public int RecipeID { get; set; }
public int? IngredientID { get; set; }
public virtual Recipe Recipe { get; set; }
public virtual Ingredient Ingredient { get; set; }
}
public class Ingredient
{
public int IngredientID { get; set; }
public string IngredientName { get; set; }
public virtual ICollection<RecipeIngredient> RecipeIngredients { get; set; }
}
私はこのブログ記事でこの問題について話しています。http://www.jasoncavett.com/2011/03/using-unobtrusive-jquery-validation.html – JasCav
現在、検証が私の最大の問題であるかどうかはわかりません。私はコントローラにデータを送信する動的フォームを取得することはできません。例えば、レシピ名を入力すると、例えばajax以外のものは、コントローラに渡されたビューモデルに存在します。 – papayt