Avoid duplicates using foreach asp.net mvc 5 - c#

I have created a tab where one adds their products and automatically it will add it on the tab. But now my issue is that it repeats the same thing on the tab view "header" whenever I have uploaded the same database. I want it to categorize it and not repeat it. Below is the code I have created.
The picture below shows the database.
Controller
public ActionResult Index()
{
return View(db.Products.ToList());
}
View
#model IEnumerable<tkkkk.Models.Product>
#{
ViewBag.Title = "Home Page";
int counterHeader = 0;
int counterBody = 0;
}
<br />
<ul class="nav nav-tabs">
#foreach (var product in Model)
{
if (counterHeader == 0)
{
<li class="active"><a data-toggle="tab" href="##product.ProductId">#product.ProductName</a></li>
}
else
{
<li><a data-toggle="tab" href="##product.ProductId">#product.ProductName</a></li>
}
counterHeader++;
}
</ul>
<br />
<div class="tab-content">
#foreach (var item in Model)
{
if (counterBody == 0)
{
<div id="#item.ProductId" class="tab-pane fade in active">
<div class="col-lg-4 col-md-6 mb-4" style="width:200px;">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="" width="200">
<div class="card-body">
<h4 class="card-title">
#item.ProductItem - #item.ProductName
</h4>
<h5>R #item.ProductPrice</h5>
</div>
</div>
</div>
</div>
}
else
{
<div id="#item.ProductId" class="tab-pane fade">
<div class="col-lg-4 col-md-6 mb-4" style="width:200px;">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="" width="200">
<div class="card-body">
<h4 class="card-title">
#item.ProductItem - #item.ProductName
</h4>
<h5>R #item.ProductPrice</h5>
</div>
</div>
</div>
</div>
}
counterBody++;
}
</div>

Related

Running Into a Object Null Error Building Bug Tracker

I am building an Bug Tracker with Asp.Net following a self paced online course. I'm running into an 'Object not set to a reference error" while attemping to list out Tickets assigned to each project in my Project Details View. I know information isn't being passed in, but I'm not sure how to fix that. Any specific solutions to the problem?
Attempted Work Arounds:
Commented out the Ticket Status and Ticket Priority Code and it worked, but it doesn't show everything I'd like.
Youtube Video of Error
Screenshot of Error
Highlighted Code in GitHub Repo
Problem Code:
#model TheBugTrackerProject.Models.Project
#using TheBugTrackerProject.Models.Enums
#using TheBugTrackerProject.Services.Interfaces
#using Microsoft.AspNetCore.Identity
#using TheBugTrackerProject.Models
#inject UserManager<BTUser> userManager
#inject IBTProjectService ProjectService
#inject IBTHistoryService HistoryService
#{
ViewData["Title"] = "Details";
BTUser btUser = await userManager.GetUserAsync(User);
BTUser projectManager = await ProjectService.GetProjectManagerAsync(Model.Id);
}
<h1>Details</h1>
<div>
<div class="container-fluid">
<div class="row gy-2">
<div class="col-md-12 col">
<div class="row col-cols-2 mt-5 bg-secondary">
<div class="card col m-1 p-2">
<div class="body">
#* Project Name *#
<h5>#Model.Name</h5>
#* Project Description *#
<p>#Model.Description</p>
<div class="progress-container progress-info m-b-25">
<span class="progress-badge" style="font-size:small">Project Status</span>
<div class="progress">
#* Razor code block *#
#{
var start = Model.StartDate.DateTime;
var end = Model.EndDate.DateTime;
var today = DateTime.Now;
var percent = today >= end ? 100 : today < start ? 0 : Math.Round((today.Subtract(start)) / (end.Subtract(start)) * 100);
}
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: #percent;">
#* Use Progress Bar code variable here *#
<span class="progress-value">#percent%</span>
</div>
</div>
</div>
</div>
</div>
<div class="card col m-1 p-2">
<div class="body">
<ul class=" list-unstyled basic-list">
<li>Start Date: <span class="">#Model.StartDate.ToString("dd MMM, yyyy")</span></li>
<li>Deadline: <span class="">#Model.EndDate.ToString("dd MMM, yyyy")</span></li>
<li>Priority: <span class="">#Model.ProjectPriority?.Name</span></li>
#* if() logic *#
#if (today < end && today >= start)
{
<li>Status:<span class="">Active</span></li>
}
else
{
<li>Status:<span class="">Inactive</span></li>
}
</ul>
</div>
</div>
</div>
</div>
<div class="col-md-4 col mt-5 ">
<div class="bg-secondary">
<div class="card m-1 p-2">
<div class="header">
<h2>Project Manager</h2>
<hr />
</div>
<div class="body" style="overflow-y:auto;height:300px;">
#* if() logic for Avatar/Default etc *#
#if (projectManager != null)
{
<div>
#if (projectManager.AvatarFileData != null)
{
<img class="rounded-circle" src="data:image/*;base64, #(Convert.ToBase64String(projectManager.AvatarFileData))" alt="" />
}
else
{
<img class="rounded-circle " style="width: 60px; height:60px;" src="https://cdn.icon-icons.com/icons2/1378/PNG/512/avatardefault_92824.png" alt="" />
}
<div>
<h5>#projectManager.FullName</h5>
<span>#projectManager.Email</span>
<p class="text-muted m-b-0">Project Manager</p>
#if (User.IsInRole(nameof(Roles.Admin)) || (btUser.Id == projectManager.Id))
{
<a class="btn btn-xs alert-primary" style="font:small;" asp-action="AssignUsers" asp-controller="Projects" asp-route-id="#Model.Id">Manage Team</a>
}
</div>
</div>
}
else
{
<div>
<img class="rounded-circle" style="width:60px;height:60px;" src="https://cdn.icon-icons.com/icons2/1378/PNG/avatardefault_92824.png" />
<div class="wid-u-info">
<h5 class="text-muted m-b-0">Not Assigned</h5>
#if (User.IsInRole(nameof(Roles.Admin)))
{
<span><a asp-action="AssignProjectManager" asp-controller="Home" asp-route-id="#Model.Id" class="btn btn-xs btn-outline-info">Assign PM</a></span>
}
<span><a asp-action="AssignProjectManager" asp-controller="Home" asp-route-id="#Model.Id" class="btn btn-xs btn-outline-info">Assign PM</a></span>
</div>
</div>
}
</div>
</div>
</div>
</div>
<div class="col-md-4 col mt-5 ">
<div class="bg-secondary">
<div class="card m-1 p-2">
<div class="header">
<h2>Project Team</h2>
<hr />
</div>
<div class="body" style="overflow-y:auto;height:300px;">
<ul class="right_chat list-unstyled mb-0">
#* Logic for avatars *#
#foreach (BTUser member in await ProjectService.GetAllProjectMembersExceptPMAsync(Model.Id))
{
<li class="">
<a href="">
<div class="media">
#if (member.AvatarFileData != null)
{
<img class="" src="data:image/*;base64,#(Convert.ToBase64String(member.AvatarFileData))" alt="" />
}
else
{
<img class="" src="https://cdn.icon-icons.com/icons2/1378/PNG/avatardefault_92824.png" />
}
<div class="">
<span class="">#member.FullName</span>
<span class="">#((await userManager.GetRolesAsync(member)).FirstOrDefault())</span>
</div>
</div>
</a>
</li>
}
</ul>
</div>
</div>
</div>
</div>
<div class="col-md-4 col mt-5 ">
<div class="bg-dark">
<div class="card m-1 p-2">
<div class="header">
<h2>Project Activity</h2>
<hr />
</div>
<div class="body" style="overflow-y:auto;height:300px;background-color:antiquewhite">
#* Project Activity loop *#
#foreach (TicketHistory history in await HistoryService.GetProjectTicketHistoryAsync(Model.Id, btUser.CompanyId.Value))
{
<div class="">
<span class="date">#history.Created.ToString("dd MM, yyyy")</span>
<h6>#history.Description</h6>
<span>By: #history.User.FullName</span>
<div class="">
<p>The Ticket <b>#history.Property</b> was edited</p>
<p>#($"Previous {history.Property}: {history.OldValue}")</p>
<p>#($"Current {history.Property}: {history.NewValue}")</p>
<div class="media">
<div class="media-body">
<h6 class="mb-0"></h6>
</div>
</div>
</div>
</div>
}
</div>
</div>
</div>
</div>
<div class="col-md-12 col">
<div class="mt-5 bg-secondary">
<div class="card m-1 p-2">
<div class="header">
<h2>Tickets</h2>
</div>
<div class="body">
<div class="table-responsive" style="overflow-y:auto;height:600px;">
<table class="table table-hover">
<thead class="">
<tr>
#* Table header *#
<th>Title</th>
<th>Developer</th>
<th>Status</th>
<th>Priority</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#* Table body *#
#foreach (var ticket in Model.Tickets.OrderByDescending(d => d.Created))
{
<tr>
<td><a asp-action="Details" asp-controller="Tickets" asp-route-id="#ticket.Id" style="color:black"><strong>#ticket.Title</strong></a> </td>
<td>
#if (ticket.DeveloperUserId != null)
{
#ticket.DeveloperUser?.FullName
}
else
{
if (User.IsInRole(nameof(Roles.Admin)) || User.IsInRole(nameof(Roles.ProjectManager)))
{
<a class="btn btn-xs btn-info" asp-action="AssignDeveloper" asp-controller="Home" asp-route-ticketId="#ticket.Id">Assign Dev</a>
}
}
</td>
#if (ticket.TicketStatus.Name == "New")
{
<td><span class="badge-success">#ticket.TicketStatus.Name</span></td>
}
else
{
<td><span class="badge-success">#ticket.TicketStatus.Name </span></td>
}
<td><span class="badge-warning">#ticket.TicketPriority.Name </span></td>
<td>#ticket.Created.ToString("MM-dd-yyyy")</td>
<td>
<a class="btn btn-sm btn-outline-info" asp-action="Details" asp-controller="Tickets" asp-route-id="#ticket.Id"><i class="fs-5 bi-justify"></i></a>
#if (ticket.DeveloperUserId == btUser.Id || ticket.OwnerUserId == btUser.Id || (projectManager?.Id == btUser.Id) || User.IsInRole(nameof(Roles.Admin)))
{
<a class="btn btn-sm btn-outline-secondary" asp-action="Edit" asp-controller="Tickets" asp-route-id="#ticket.Id"><i class="fs-5 bi-pencil"></i></a>
}
#if (User.IsInRole("Admin") || (projectManager?.Id == btUser.Id))
{
<a class="btn btn-sm btn-outline-danger"><i class="fs-5 bi-archive"></i></a>
}
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Thanks in advance.
P.S. My code is a mess; but looking forward to working with other's on here and possibly in the future.
In your project service, you aren't Include-ing the ticket status so it will be null. Try something like this instead:
Project project = await _context.Projects
.Include(p => p.Tickets)
.ThenInclude(t => t.TicketStatus) // <-- add this line
.Include(p => p.Members)
.Include(p => p.ProjectPriority)
.FirstOrDefaultAsync(p => p.Id == projectId && p.CompanyId == companyId);

How to display 3 Bootstrap Card titles in same row using foreach loop

I have view where I need to display 3 card tiles in a same row. But at present it displays one after the other. How Do I display 3 card tiles in same row?
Index.cshtml -
#model List<JobPortal.Models.MyDB>
#{
ViewBag.Title = "Current List";
}
<div class="row">
#foreach (var list in Model)
{
<div class="flexcontainer">
#Html.Partial("_MySharedView", list)
</div>
}
</div>
Shared view -
#model JobPortal.Models.MyDB
#if (Model.JDetails.Count > 0)
{
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<p class="card-text">Model.details</p>
</div>
</div>
}
You can do something like this in your partial view:
#if(Model.JDetails.Count > 0)
foreach (var item in Model.JDetails)
{
<div class="col-sm">
<div class="card">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<p class="card-text">#item.details</p>
</div>
</div>
</div>
}
}

Load More Button Umbraco, Razor funcionality

im new to umbraco and c#, I made a blog list in umbraco razor but how can i make a load more button funcionality?
i want when the user click the button load more 5 items to the list.
One way is the the users click the button update the query, but i would need to refresh the page.
Any Help would be great since im stuck on this one, and have no clue how to do this in umbraco.
#{
var selection = Model.Content.Children().Where(x => x.IsVisible()).Take(5).ToList();
}
<div class="container-fluid" style="padding-left: 117px;">
<div class="row" style="margin-top: 5em;">
<div class="col-lg-9 col-md-9 col-sm-9">
<!-- BLOG START -->
#while(selection.Any()){
var oneItem = selection.First();
selection.Remove(oneItem);
<div class="row">
<div class="col-md-4 col-sm-12" onclick="location.href='#oneItem.Url'" style="cursor:pointer">
<div class="card">
<img src="#Umbraco.TypedMedia(oneItem.GetPropertyValue<int>("imagemPublicacaoBlog")).Url" style="height: 15em;">
</div>
</div>
<div class="col-md-6 col-sm-12" onclick="location.href='#oneItem.Url'" style="cursor:pointer">
<span class="card-text qs-blog-direcao">#oneItem.GetPropertyValue("tipoDeDirecao")</span><br><br>
<span class="qs-blog-date-1page" id="qs-datetime">#(oneItem.GetPropertyValue<DateTime>("dataDePublicacaoBlog").ToString("dd MMMM yyyy",new CultureInfo("pt-PT")))</span>
<br>
<span class="qs-blog-publicado-por">#oneItem.GetPropertyValue("publicadoPorBlog") - OPINIÃO </span>
<span class="qs-blog-titulo-1page">#oneItem.GetPropertyValue("tituloBlog")</span>
</div>
<div class="col-md-12 col-sm-12 onclick="location.href='#oneItem.Url'" style="cursor:pointer"" style="margin-top:2em;">
<span class="qs-blog-resumo d-flex justify-content-start">#oneItem.GetPropertyValue("resumoBlog")</span>
<span class="d-flex justify-content-end"><a><img src=" /media/1027/icon_inf_verde.png"></a></span>
</div>
</div>
var twoItems = selection.Take(2).ToList();
if(twoItems.Any()){
<div class="row">
#foreach (var item in twoItems){
selection.Remove(item);
<div class="col-md-6 col-sm-12" onclick="location.href='#item.Url'" style="cursor:pointer">
<span class="card-text qs-blog-direcao-double">#item.GetPropertyValue("tipoDeDirecao")</span><br>
<img src="#Umbraco.TypedMedia(item.GetPropertyValue<int>("imagemPublicacaoBlog")).Url" style="height: 10em;margin-top: 2em;">
<div class="qs-blog-sideByside">
<span class="qs-blog-date-1page-double" id="qs-datetime">#(item.GetPropertyValue<DateTime>("dataDePublicacaoBlog").ToString("dd MMMM yyyy",new CultureInfo("pt-PT")))</span><br>
<span class="qs-blog-publicado-por-double"> #item.GetPropertyValue("publicadoPorBlog") - OPINIÃO </span>
</div>
<div class="qs-blog-titulo-1page-double">#item.GetPropertyValue("tituloBlog")</div>
<div class="qs-blog-resumo-blog d-flex justify-content-start">#item.GetPropertyValue("resumoBlog")</div>
<span class="d-flex justify-content-end"><a><img src=" /media/1027/icon_inf_verde.png"></a></span>
</div>
}
</div>
<br>
}
}
<!-- BLOG END -->
</div>
It could be something like this:
#{
int page = int.TryParse(Request["page"], out page) ? page : 0;
int pageSize = 5;
var selection = Model.Content
.Children()
.Where(x => x.IsVisible())
.Skip(page * pageSize)
.Take(pageSize)
.ToList();
}
<div class="container-fluid">
...
</div>
<a href="#Request.RawUrl.Split('?')[0]?page=#(page + 1)">
Load next #pageSize results
</a>

Umbraco C# - Display 2 different columns on foreach loop (1-2) (1-2)

I'm trying to display data from the Umbraco in this order 1-2 in foreach loop (bootstarp columns), but my code is displaying in this order of columns 1-1, instead of 1-2 on loop. It does not add the second column to the second row just one column.
E.g. 1 item, then 2 items, then 1 item again with different columns displays.
whats the better approach to make 1-2 on loop of content?
I'm kinda lost on this one. Any help would be good.
<div class="container-fluid" style="padding-left: 117px;">
<div class="row" style="margin-top: 5em;">
<div class="col-lg-9 col-md-9 col-sm-9">
<!-- BLOG START-->
#foreach(var data in Model.Content.Children().Where(x => !x.GetPropertyValue<bool>("umbracoNaviHide"))){
if(data.HasValue("blogNested")){
var items = data.GetPropertyValue<IEnumerable<IPublishedContent>>("blogNested");
int i = 0 ;
foreach(var item in items){
<div class="row">
#if(i % 2 == 0) {
<div class="col-md-4 col-sm-12">
<div class="card">
<img src=" /media/1180/dude.jpg" style="height: 15em;">
</div>
</div>
<div class="col-md-6 col-sm-12">
<span class="card-text qs-blog-direcao">#(item.GetPropertyValue<string>("tipoDeDirecao"))</span><br><br>
<span class="qs-blog-date-1page">#(item.GetPropertyValue<string>("dataDePublicacaoBlog"))</span><br>
<span class="qs-blog-publicado-por"> #(item.GetPropertyValue<string>("publicadoPorBlog")) - OPINIÃO </span><br><br><br>
<span class="qs-blog-titulo-1page">#(item.GetPropertyValue<string>("tituloBlog"))</span>
</div>
<div class="col-md-12 col-sm-12" style="margin-top:2em;">
<span class="qs-blog-resumo d-flex justify-content-start">#(item.GetPropertyValue<string>("resumoBlog"))</span>
<span class="d-flex justify-content-end"><a><img src=" /media/1027/icon_inf_verde.png"></a></span>
</div>
}
else {
<div class="col-md-6 col-sm-12">
<span class="card-text qs-blog-direcao-double">#(item.GetPropertyValue<string>("tipoDeDirecao"))</span><br>
<img src=" /media/1182/tempo.jpg" style="height: 10em;margin-top: 2em;">
<div class="qs-blog-sideByside">
<span class="qs-blog-date-1page-double">#(item.GetPropertyValue<string>("dataDePublicacaoBlog"))</span><br>
<span class="qs-blog-publicado-por-double"> #(item.GetPropertyValue<string>("publicadoPorBlog")) - OPINIÃO </span>
</div>
<div class="qs-blog-titulo-1page-double">#(item.GetPropertyValue<string>("tituloBlog"))</div>
<div class="qs-blog-resumo-blog d-flex justify-content-start">#(item.GetPropertyValue<string>("resumoBlog"))</div>
<span class="d-flex justify-content-end"><a><img src=" /media/1027/icon_inf_verde.png"></a></span>
</div>
}
#{i++;}
</div>
}
}
}
<!-- BLOG START-->
</div>
It could be something like this
var items = data.GetPropertyValue<IEnumerable<IPublishedContent>>("blogNested")
.ToList();
while (items.Any())
{
var oneItem = items.First();
items.Remove(oneItem);
<div class="row">
<div class="col-md-12">
#oneItem.Id
</div>
</div>
var twoItems = items.Take(2).ToList();
if (twoItems.Any())
{
<div class="row">
#foreach (var item in twoItems)
{
items.Remove(item);
<div class="md-6">
#item.Id
</div>
}
</div>
}
}
This will render
<div class="row">
<div class="col-md-12">
<text>{{Id}}</text>
</div>
</div>
<div class="row">
<div class="md-6">
<text>{{Id}}</text>
</div>
<div class="md-6">
<text>{{Id}}</text>
</div>
</div>
<div class="row">
<div class="col-md-12">
<text>{{Id}}</text>
</div>
</div>
...

Showing the data of the right side of the TimeLine

How can I show the description Data on the left or the right side of the timeline acording of it Candidate.Saving result?
If it's Candidate.Saving == true it should be on the left, if it's Candidate.Saving==false it should be on the right side of the time line.
I will shou here my candidate Model And my View.
public class Candidate : BaseEntity
{
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; }
}
Here is my 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>
#foreach (var Description in Model.Description)
{
#if (Model.Saving == true)
{
<ul>
<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">Title</h4>
<p>
<small class="text-muted"><i class="fa fa-clock-o"></i> #Model.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>#Model.Description</p>
</div>
</div>
</li>
</ul>
}
#if (Model.Saving == false)
{
<ul>
<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> #Model.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>#Model.Description</p>
</div>
</div>
</li>
</ul>
}
}
</div>
#*<div class="panel-body">
<ul class="timeline">
<li>
<li class="timeline-inverted">
Isto é para inverter o lado
<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> #Model.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>#Model.Description</p>
</div>
</div>
</li>
</ul>
</div>
</div>*#
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="#Model.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>
What I've tried to do:
I have tried to make a foreach method where the side depends of the Candidate.Saving, But the result was this:
I have managed to fix it.
This is how I did It:
<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">
#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">Title</h4>
<p>
<small class="text-muted"><i class="fa fa-clock-o"></i>
#Model.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>#Model.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> #Model.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>#Model.Description</p>
</div>
</div>
</li>
</ul>
}
</div>
</div>
I only have on doubt right now, to be able to add more than one Description for the candidate I should Create a list off Descriptions
and do a foreach method in the view right?
Right now my controller looks like this:
public IActionResult CandidateHistory(int Id)
{
using (var applicationcontext = new ApplicationContext())
{
var candidate = applicationcontext.Candidates.Where(s => s.Id.Equals(Id)).SingleOrDefault();
if (candidate == null)
{
return NotFound();
}
Candidate candidates = new Candidate();
candidates.DescriptionList.Add(candidate);
candidates.DescriptionList.Add(new Candidate { Description = candidate.Description });
candidates.DescriptionList.Add(new Candidate { Title = candidate.Title });
candidates.DescriptionList.Add(new Candidate { DateOfDescription = candidate.DateOfDescription });
candidates.DescriptionList.Add(new Candidate { Saving = candidate.Saving });
applicationcontext.Candidates.Add(candidates);
return View();
}
}
[HttpPost, ActionName("CandidateHistory")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CandidateHistoryPost(int? id, Candidate candidatelist)
{
if (id == null)
{
return NotFound();
}
using (var context = new ApplicationContext())
{
var candidateUpdate = await context.Candidates.SingleOrDefaultAsync(s => s.Id == id);
if (await TryUpdateModelAsync<Candidate>(candidateUpdate, "", s => s.Description, s => s.Title, s => s.DateOfDescription, s => s.Saving))
{
try
{
await context.SaveChangesAsync();
return RedirectToAction(nameof(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(candidateUpdate);
}
}
I'm trying to output an Candidate.DescriptionList to the CandidateHistory View so that way I can save into that list all the descriptions, description title, date and "saving", so i can do a foreach method to output all the description of that user.
But now I have this error:
System.NullReferenceException: 'Object reference not set to an instance of an object.
HCCBPOHR.Data.Candidate.DescriptionList.get returned null.`
I have managed to fix it.
This is how I did It:
<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">
#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">Title</h4>
<p>
<small class="text-muted"><i class="fa fa-clock-o"></i>
#Model.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>#Model.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> #Model.DateOfDescription.ToShortDateString()</small>
</p>
</div>
<div class="timeline-body">
<p>#Model.Description</p>
</div>
</div>
</li>
</ul>
}
</div>
</div>
I only have on doubt right now, to be able to add more than one Description for the candidate I should Create a list off Descriptions
and do a foreach method in the view right?

Categories