将数据导航到具有相同模型和Foreach方法的列表中

时间:2018-07-04 09:11:20

标签: c# asp.net asp.net-core

我想做什么:**

在视图中使用candidate.Description保存数据后,将candidate.Titlecandidate.DateOfDescriptioncandidate.SavingForeach保存到候选模型内的列表中( 我已经完成了ForEach )。

Candidate.Cs(Model):

   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>();
    }

我的CandidateController:

       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是在视图{{1}中接收输入数据的值(candidate.Descriptioncandidate.Titlecandidate.DateOfDescriptioncandidate.Saving) }}正在从数据库(var candidatesCandidate.IdCandidate.NameCandidate.ProfileText等(除Description / Title /保存/ dateofdescription外的所有内容)中接收数据

使用此代码,我只有一个问题:

  

它会创建所有描述并将其添加到列表中,但最后还会创建并清空候选者(不应这样做)。

如您所见,这个“空”的候选人正在保存描述/历史enter image description here **

CandidateCv收到此值enter image description here  并且Candidate candidate收到此信息:enter image description here enter image description here

0 个答案:

没有答案