У меня такая проблема: у меня 2 модели. Модель задачи и модель предложения. Задание состоит из предложений. Например, в 1 задаче 10 предложений. Я хотел отредактировать все в одном окне. У меня нет проблем с отображением, потому что он отображается правильно (например, 1 задача и 10 принадлежащих ей предложений). Но когда я хочу отредактировать предложение (любой из его элементов: Mistery, Before, After ...), изменение не сохраняется в базе данных. Могу я попросить помощи, что делаю не так?
Код:
@model Languages.ViewModels.TaskViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@foreach (var sentence in Model.SentencesForTaskViewModel)
{
@Html.HiddenFor(x => sentence.Mystery)
@Html.HiddenFor(x => sentence.SentenceForTaskId)
@Html.HiddenFor(x => x.TaskId)
@Html.HiddenFor(x => x.LessonId)
@Html.HiddenFor(x => x.NameOfTask)
@Html.HiddenFor(x => x.MaxIloscPkt)
@Html.HiddenFor(x => x.PunktyZdobytePrzezUsera)
@Html.HiddenFor(x => x.Wynik)
@Html.HiddenFor(x => x.SentencesForTask)
@Html.HiddenFor(x => x.SentenceForTaskViewModel)
<div class="form-group">
@Html.LabelFor(model => sentence.TaskId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => sentence.TaskId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => sentence.TaskId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => sentence.Before, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => sentence.Before, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => sentence.Before, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => sentence.Mystery, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => sentence.Mystery, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => sentence.Mystery, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => sentence.After, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => sentence.After, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => sentence.After, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
</div>
}
}
//////////////////////////////////////////
controller:
[HttpPost]
public ActionResult RozwiazywanieUzupelnianki(TaskViewModel taskVM)
{
int id = taskVM.TaskId;
var sentencesForTask = db.SentencesForTask.Where(x => x.TaskId == id).ToList();
List<SentenceForTaskViewModel> sentencesForTaskVM = new List<SentenceForTaskViewModel>();
foreach (var item in sentencesForTask)
{
sentencesForTaskVM.Add(new SentenceForTaskViewModel()
{
TaskId = item.TaskId,
Before = item.Before,
Mystery = item.Mystery,
SolutionProposal = item.SolutionProposal,
After = item.After,
SentenceForTaskId = item.SentenceForTaskId,
});
}
taskVM.SentencesForTaskViewModel = sentencesForTaskVM;
using (ApplicationDbContext db = new ApplicationDbContext())
{
sentencesForTask = db.SentencesForTask.Where(x => x.TaskId == id).ToList();
SentenceForTaskViewModel sentenceVM = new SentenceForTaskViewModel();
Uzupelnianka uzupelnianka = db.Uzupelnianki.Find(id);
var sentenceForTask = new ZdanieDoUzupelnianki();
var zdaniaVM3 = taskVM.SentencesForTaskViewModel;
foreach (var item in zdaniaVM3)
{
sentenceForTask.TaskId = item.TaskId;
sentenceForTask.SentenceForTaskId = item.SentenceForTaskId;
sentenceForTask.Before = item.Before;
sentenceForTask.Mystery = item.Mystery;
sentenceForTask.After = item.After;
sentenceForTask.SolutionProposal = item.SolutionProposal;
db.SaveChanges();
}
return RedirectToAction("xxxxxxxxx", new { taskId = taskVM.TaskId });
}