【发布时间】:2014-07-07 17:00:12
【问题描述】:
我正在创建一个新的食谱输入表单,我发现了两个有趣的问题:
- 成分不会发布到配方集合中,并且
- 当我将参数设置为集合时,表单只会发布一个成分并发送 null。
理想情况下,我希望成分集合位于配方项下,但我会满足于让成分集合写入参数。
以下是两个模型(由实体框架生成):
食谱:
public int recipeId { get; set; }
[Required]
[StringLength(255)]
[DisplayName("Name")]
public string name { get; set; }
[DisplayName("Category")]
public int? mealCategoryId { get; set; }
[DisplayName("Preperation Time")]
public int? prepTime { get; set; }
[DisplayName("Cook Time")]
public int? cookTime { get; set; }
[DisplayName("Stand Time")]
public int? standTime { get; set; }
[DisplayName("Serving Size")]
public int? servingSize { get; set; }
[DisplayName("Status")]
public int recipeStatusId { get; set; }
[DisplayName("Description")]
[UIHint("Multiline")]
public string description { get; set; }
public virtual ICollection<Direction> Directions { get; set; }
public virtual ICollection<Image> Images { get; set; }
public virtual ICollection<Ingredient> Ingredients { get; set; }
public virtual MealCategory MealCategory { get; set; }
public virtual ICollection<Nutrition> Nutritions { get; set; }
public virtual ICollection<Rating> Ratings { get; set; }
public virtual RecipeStatus RecipeStatus { get; set; }
}
成分:
public partial class Ingredient
{
public int ingredientId { get; set; }
public int? recipeId { get; set; }
public int? subCategoryId { get; set; }
public int measurementId { get; set; }
public int amount { get; set; }
public virtual Recipe Recipe { get; set; }
public virtual SubCategory SubCategory { get; set; }
public virtual Measurement Measurement { get; set; }
}
查看代码:
<div class="form-horizontal">
<h4>Ingredients</h4>
<hr />
<table class="table IngredientList">
<tr>
<th>
@Html.LabelFor(model => model.Recipe.Ingredients)
</th>
<th>
@Html.LabelFor(model => model.SelectedMeasurementId)
</th>
<th>
@Html.Label("Amount")
</th>
</tr>
@for (int i = 0; i < Model.Recipe.Ingredients.Count; i++)
{
<tr>
<td>
@Html.DropDownListFor(m => Model.Recipe.Ingredients.ElementAt(i).ingredientId, Model.SubIngredients, new { @class = "form-control input-block-level" })
</td>
<td>
@Html.DropDownListFor(m => Model.Recipe.Ingredients.ElementAt(i).measurementId, Model.Measurements, new { @class = "form-control input-block-level" })
</td>
<td>
@Html.TextBoxFor(m => Model.Recipe.Ingredients.ElementAt(i).amount, new { @class = "form-control input-block-level" })
</td>
</tr>
}
</table>
</div>
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Recipe recipe, int SelectedCategoryId, List<Ingredient> Ingredients)
{
recipe.mealCategoryId = SelectedCategoryId;
recipe.recipeStatusId = 1;
if (ModelState.IsValid)
{
dbContext.Entry(recipe).State = EntityState.Added;
dbContext.SaveChanges();
return RedirectToAction("Details", new {id = recipe.recipeId});
}
return View(recipe);
}
因此,如果我将参数设置为 List Ingredients,则返回 null。如果我将其设置为成分成分,那么我只会得到四种成分中的第一种。 (我将模型设置为在加载时创建 4 个成分记录。)
我的问题是:如何让它通过成分集合?
编辑:
我已确认请求将所有 4 种成分发送过来。绑定好像有问题。
【问题讨论】:
-
您有什么理由选择在模型定义中使用
ICollection<Ingredients>而不是List<Ingredients> -
@C Sharper 由实体框架生成。我不确定改变它是否有任何影响。但是,我已经尝试过这样做,但并没有解决问题。
-
不要使用 EF 生成的模型作为您的“ViewModels”,因此这是不好的做法。创建您自己的模型类并为它们提供所需的属性。
标签: c# asp.net asp.net-mvc controller