Что я хочу сделать: **
Сохраните candidate.Description
, candidate.Title
, candidate.DateOfDescription
, candidate.Saving
в список внутри модели кандидата, после сохранения данных с помощью Foreach
в представлении ( Я уже сделал ForEach ).
Candidate.Cs (модель):
public int Id { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public string ProfileText { get; set; }
public Byte[] CV { get; set; }
public string CVNAME { get; set; }
public List<Profile> ProfileList { get; set; }
public String Description { get; set; }
public Boolean Saving { get; set; }
public string Title { get; set; }
public DateTime DateOfDescription { get; set; }
public List<Candidate> DescriptionList { get; set; }
public Candidate()
{
DescriptionList = new List<Candidate>();
}
Мой КандидатКонтроллер:
public IActionResult CandidateHistory(int Id)
{
using (var applicationcontext = new ApplicationContext())
{
var candidate = applicationcontext.Candidates.AsNoTracking().Include(q => q.DescriptionList).Single(q => q.Id == Id); //Recieving all the data from the Candidate With the ID = Id
if (candidate == null) //if Candidate isn't found return a error page
{
return NotFound();
}
return View(candidate);
}
}
[HttpPost, ActionName("CandidateHistory")]
[ValidateAntiForgeryToken]
public IActionResult CandidateHistoryPost([Bind("Description, Title, DateOfDescription, Saving")]Candidate candidate, int Id)
{
try
{
if (ModelState.IsValid)
{
using (var applicationContext = new ApplicationContext())
{
var candidates = applicationContext.Candidates.AsNoTracking().Include(q => q.DescriptionList).Single(q => q.Id == Id); //Getting all the DATA from the Candidate with the Id passed in the Get Method
candidates.DescriptionList.Add(new Candidate {
Description = candidate.Description,
Title = candidate.Title,
DateOfDescription = candidate.DateOfDescription,
Saving = candidate.Saving }); //Adds to the DescrptionList all the Description Data Needed
applicationContext.Candidates.Update(candidates);
applicationContext.SaveChanges();
return RedirectToAction("CandidateHistory");
}
}
}
catch (DbUpdateException ex )
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Unable to save changes. " +"Try again, and if the problem persists " +"see your system administrator.");
}
return View();
}
And Also CandidateHistory View:
@model HCCBPOHR.Data.Candidate
@{
ViewData["Title"] = "CandidateHistory";
}
<h2>Canidate - @Model.Name</h2>
<label>History</label>
<hr />
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-clock-o fa-fw"></i> History Of @Model.Name
</div>
<div class="panel-body">
@foreach (var Description in Model.DescriptionList)
{
@if (Model.Saving == true)
{
<ul class="timeline">
<li class="timeline">
<div class="timeline-badge">
<i class="fa fa-check"></i>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="timeline-title">@Description.Title</h4>
<p>
<small class="text-muted"><i class="fa fa-clock-o"></i> @Description.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>@Description.Description</p>
</div>
</div>
</li>
</ul>
}
@if (Model.Saving == false)
{
<ul class="timeline">
<li class="timeline-inverted">
<div class="timeline-badge">
<i class="fa fa-check"></i>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="timeline-title">Title</h4>
<p>
<small class="text-muted"><i class="fa fa-clock-o"></i> @Description.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>@Description.Description</p>
</div>
</div>
</li>
</ul>
}
}
</div>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@Description.Saving"> Add History Description</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<form asp-action="CandidateHistory">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Description" class="control-label">
</label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label>Selects</label>
<select asp-for="Saving" class="form-control">
<option value="false">Candidate </option>
<option value="true">Hitachi</option>s
</select>
</div>
<div class="form-group">
<label asp-for="DateOfDescription" class="form-group">
</label>
<input asp-for="DateOfDescription" class="form-group" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button></div>
</div>
</div>
</div>
Candidate candidate
- это получение значения введенных данных в представлении (candidate.Description
, candidate.Title
, candidate.DateOfDescription
, candidate.Saving
), var candidates
- получение данных из БД (Candidate.Id
, Candidate.Name
, Candidate.ProfileText
, CandidateCv
и т. Д. (Все, кроме описания / заголовка / сохранения / даты описания))
С этим кодом у меня проблема только:
Создает и добавляет в список все описания, но в конце также создает и пустой кандидат (не должен этого делать).
как видите, "пустые" кандидаты сохраняют Descriptin / History **
Candidate candidate
получает это значение
и candidates
получает это: