与此有关的问题:。 由于具体投入领域,没有将观点转换成控制器。
然而,我以前认为,由于在前一个问题上的帮助,我现在有一个明确的项目(因此,我假定,正如我所指出的,这将是一个可再生的最低限度解决办法),而增加了一些要素和看法模式却被丢失,而有150多个要素可以转而采用POST方法。 没有任何其他错误。
当我设定了<代码>i < 100时,该编码即行其用,当时我将其定为150个,它就失去了该观点模式。
当数据来自数据库时,也出现了同样的问题——我尝试通过50套内容(使用<条码>、<<>>/代码>和<代码>Skip)的观察模型,而对于其中每一种内容,则采用“观点模型”(因此与数据本身不成问题)。
Is there some sort of limit to that that I cannot find?
主计长:
public IActionResult Edit()
{
var r1 = new RecipeDTO() { Id = 1, TotalKcal = 10, TotalGL = 45, RecipeDayId = 1, MealId = 1, Name = "QQ" };
var c1 = new List<类别>() { new 类别() { Id = 1, Name = "AA" }, new 类别() { Id = 2, Name = "BB" } };
var s1 = new List<缩略语>() {
new 缩略语() { 类别Id = 1, Id = 160, IsChecked = true, Name = "AA", Grams = 1, TotalKcal = 1, GL = 1.0 },
new 缩略语() { 类别Id = 1, Id = 162, IsChecked = true, Name = "BB", Grams = 1, TotalKcal = 1, GL = 1.0 }
};
for (var i = 0; i < 150; i++)
{
s1.Add(new 缩略语() { 类别Id = 1, Id = i + 1, IsChecked = true, Name = $"AA{i}", Grams = 5, TotalKcal = 5, Amount = 0 });
}
var model = new Recipe观点Model() { Recipe = r1, Categories = c1, SelectedCheckboxes = s1 };
return 观点("Create", model);
}
[HttpPost]
public IActionResult Edit(Recipe观点Model model)
{
return 观点("Create", model);
}
加入:
public class RecipeDTO
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int MealId { get; set; }
public int RecipeDayId { get; set; }
public List<IngredientDTO> Ingredients { get; set; } = new List<IngredientDTO>();
public IEnumerable<Recipe_Ingredients> Recipe_Ingredients { get; set; } = new List<Recipe_Ingredients>();
[DisplayName("Selected amount of kcals")]
public double TotalKcal
{
get
{
double totalKcal = 0;
Ingredients.ForEach(item =>
{
totalKcal += item.Kcals;
});
return totalKcal;
}
set { }
}
public double TotalGL
{
get
{
double totalGL = 0;
Ingredients.ForEach(item =>
{
totalGL += item.GL;
});
return totalGL;
}
set { }
}
}
类别
public class 类别
{
public int Id { get; set; }
public string Name { get; set; }
}
缩略语
public class 缩略语
{
public int? Id { get; set; }
public bool IsChecked { get; set; }
public string Name { get; set; }
public int 类别Id { get; set; }
public int Grams { get; set; }
public double Amount { get; set; }
public Product Product { get; set; } = new Product();
public int ProductId { get; set; }
public int TotalKcal
{
get
{
return Product.KcalPer100g * (Grams / 100);
}
set { }
}
public double GL
{
get
{
return Math.Floor((Product.GI * (Product.CarbsPer100g * (Grams / 100)) / 100));
}
set { }
}
}
Recipe观点Model
public class Recipe观点Model
{
public RecipeDTO Recipe { get; set; } = new RecipeDTO();
public List<RecipeDTO> Recipes { get; set; } = new List<RecipeDTO>();
public List<缩略语> SelectedCheckboxes { get; set; }
public IEnumerable<类别> Categories { get; set; }
public bool IsIngredientBeingSwitched { get; set; } = false;
}
观点
@model Recipe观点Model
@{
var url = Model.Recipe.Id == 0 ? Url.Action("Edit", "Home") : Url.Action("Edit", "Home");
url = Model.IsIngredientBeingSwitched && Model.Recipe.Id != 0 ? Url.Action("Edit", "Home") : URL;
}
<div class="container">
<form method="post" action="@url">
<div class="mb-3 row">
<label asp-for="@Model.Recipe.TotalKcal"></label>
<input id="totalRecipe" asp-for="@Model.Recipe.TotalKcal" class="form-control" readonly />
<label asp-for="@Model.Recipe.TotalGL"></label>
<input id="totalRecipeGL" asp-for="@Model.Recipe.TotalGL" class="form-control" readonly />
</div>
<div class="mb-3 row">
<input hidden asp-for="@Model.Recipe.Id" />
<input hidden asp-for="@Model.Recipe.MealId" />
<input hidden asp-for="@Model.Recipe.RecipeDayId" />
<label asp-for="@Model.Recipe.Name"></label>
<input asp-for="@Model.Recipe.Name" class="form-control" />
<span asp-validation-for="@Model.Recipe.Name" class="text-danger"></span>
</div>
@foreach (类别 category in Model. Categories)
{
<text>
<div class="m-2"><h3>@category.Name</h3></div>
<div class="row">
@for (var i = 0; i < Model.SelectedCheckboxes.Count(); i++)
{
if (Model.SelectedCheckboxes[i].类别Id == category.Id)
{
<text>
<div class="col-auto">
<div>
<input class="form-number-input" asp-for="@Model.SelectedCheckboxes[i].IsChecked" type="checkbox">
<input hidden asp-for="@Model.SelectedCheckboxes[i].Id" />
<label class="form-check-label" asp-for="@Model.SelectedCheckboxes[i].IsChecked">@Model.SelectedCheckboxes[i].Name</label>
</div>
<div>
<div>
<label class="form-check-label" asp-for="@Model.SelectedCheckboxes[i].Grams">Amount: </label>
<input class="form-control-sm" asp-for="@Model.SelectedCheckboxes[i].Grams" />
</div>
<div>
<label class="form-check-label" asp-for="@Model.SelectedCheckboxes[i].TotalKcal">Selected kcals: </label>
<input class="form-control-sm total-kcal" asp-for="@Model.SelectedCheckboxes[i].TotalKcal" readonly>
</div>
<div>
<label class="form-check-label" asp-for="@Model.SelectedCheckboxes[i].GL">Load: </label>
<input class="form-control-sm total-gl" asp-for="@Model.SelectedCheckboxes[i].GL" readonly>
</div>
</div>
</div>
</text>
}
}
</div>
</text>
}
<button type="submit" class="btn btn-primary mt-5">Save</button>
</form>
</div>