I have Two Models CustomerLead model and LeadRemarks model. I have created views for CustomerLead Details view And Upsert view, what I want To do is get Remarks from the LeadRemarks model which is referenced by CustomerLeadID in LeadRemarks Model. and display those remarks in my CustomerLead Upsertview and be able to add new remarks against CustomerLead Id in LeadRemarks as well.I'm using Repository Pattern
CustomerLead Model:
public class CustomerLead
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
public string PhoneNumber { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string Sector { get; set; }
[Required]
public string TicketType { get; set; }
public int? FromLocationsId { get; set; }
[ForeignKey("FromLocationsId")]
public FromLocations? FromLocations { get; set; }
public int? ToLocationsId { get; set; }
[ForeignKey("ToLocationsId")]
public ToLocations? ToLocations { get; set; }
public string LeadStatus { get; set; }
public DateTime CreatedOn { get; set; }
[Required]
public DateTime CallBackTime { get; set; }
public string? Comments { get; set; }
[Required]
public string CreatedBy { get; set; }
public string? ApplicationUserId { get; set; }
[ForeignKey("ApplicationUserId")]
public ApplicationUser? ApplicationUser { get; set; }
}
LeadRemarks Model:
public class LeadRemarks
{
public int Id { get; set; }
public string Remarks { get; set; }
public DateTime CreatedOn { get; set; }
public int? CustomerLeadId { get; set; }
[ForeignKey("CustomerLeadId")]
public CustomerLead? CustomerLead { get; set; }
}
CustomerLead Controller:
public class CustomerLeadController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public CustomerLeadController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
var claimsIdentity = (ClaimsIdentity)User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
if (claim != null)
{
var user = _unitOfWork.CustomerLead
.GetAll(c => c.ApplicationUserId == claim.Value)
.ToList();
}
return View();
}
public IActionResult Upsert(int? id)
{
CustomerLeadVm customerleadVM = new CustomerLeadVm()
{
CustomerLead = new CustomerLead(),
FromLocationsList = _unitOfWork.FromLocations.GetAll().Select(i => new SelectListItem
{
Text = i.FromLocation,
Value = i.Id.ToString()
}),
ToLocationsList = _unitOfWork.ToLocations.GetAll().Select(i => new SelectListItem
{
Text = i.ToLocation,
Value = i.Id.ToString()
})
};
//var claimsIdentity = (ClaimsIdentity)User.Identity;
//var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
if (id == null)
{
//this is for create
return View(customerleadVM);
}
//this is for edit
customerleadVM.CustomerLead = _unitOfWork.CustomerLead.Get(id.GetValueOrDefault());
if (customerleadVM.CustomerLead == null)
{
return NotFound();
}
return View(customerleadVM);
}
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public IActionResult Upsert(CustomerLeadVm customerleadVM)
{
//var errors = ModelState.Values.SelectMany(v => v.Errors);
if (ModelState.IsValid)
{
var claimsIdentity = (ClaimsIdentity)User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
customerleadVM.CustomerLead.ApplicationUser = _unitOfWork.ApplicationUser
.GetFirstOrDefault(c => c.Id == claim.Value);
if (customerleadVM.CustomerLead.Id == 0)
{
_unitOfWork.CustomerLead.Add(customerleadVM.CustomerLead);
customerleadVM.CustomerLead.CreatedOn = DateTime.Now;
customerleadVM.CustomerLead.CallBackTime = DateTime.Now;
}
else
{
_unitOfWork.CustomerLead.Update(customerleadVM.CustomerLead);
customerleadVM.CustomerLead.CreatedOn = DateTime.Now;
customerleadVM.CustomerLead.CallBackTime = DateTime.Now;
}
_unitOfWork.Save();
return RedirectToAction(nameof(Index));
}
else
{
customerleadVM.FromLocationsList = _unitOfWork.FromLocations.GetAll().Select(i => new SelectListItem
{
Text = i.FromLocation,
Value = i.Id.ToString()
});
customerleadVM.ToLocationsList = _unitOfWork.ToLocations.GetAll().Select(i => new SelectListItem
{
Text = i.ToLocation,
Value = i.Id.ToString()
});
if (customerleadVM.CustomerLead.Id != 0)
{
customerleadVM.CustomerLead = _unitOfWork.CustomerLead.Get(customerleadVM.CustomerLead.Id);
}
}
return View(customerleadVM);
}
#region API CALLS
[HttpGet]
public IActionResult GetAll(CustomerLeadVm customerLeadVm)
{
if (User.IsInRole(SD.Role_Lead_Data))
{
var claimsIdentity = (ClaimsIdentity)User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
var allObj = _unitOfWork.CustomerLead.GetAll(c => c.ApplicationUserId == claim.Value, includeProperties: "FromLocations,ToLocations").ToList();
return Json(new { data = allObj });
}
else
{
var allObj = _unitOfWork.CustomerLead.GetAll( includeProperties: "FromLocations,ToLocations").ToList();
return Json(new { data = allObj });
}
}
#endregion
}
}
LeadRemarks Controller:
public class LeadRemarksController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public LeadRemarksController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
return View();
}
public IActionResult Upsert(int? id)
{
LeadRemarks leadremarks = new LeadRemarks();
if (id == null)
{
//this is for create
return View(leadremarks);
}
//this is for edit
leadremarks = _unitOfWork.LeadRemarks.Get(id.GetValueOrDefault());
if (leadremarks == null)
{
return NotFound();
}
return View(leadremarks);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert(LeadRemarks leadremarks)
{
if (ModelState.IsValid)
{
if (leadremarks.Id == 0)
{
_unitOfWork.LeadRemarks.Add(leadremarks);
}
else
{
_unitOfWork.LeadRemarks.Update(leadremarks);
}
_unitOfWork.Save();
return RedirectToAction(nameof(Index));
}
return View(leadremarks);
}
#region API CALLS
[HttpGet]
public IActionResult GetAll()
{
var allObj = _unitOfWork.LeadRemarks.GetAll();
return Json(new { data = allObj });
}
[HttpDelete]
public IActionResult Delete(int id)
{
var objFromDb = _unitOfWork.LeadRemarks.Get(id);
if (objFromDb == null)
{
return Json(new { success = false, message = "Error while deleting" });
}
_unitOfWork.LeadRemarks.Remove(objFromDb);
_unitOfWork.Save();
return Json(new { success = true, message = "Delete Successful" });
}
#endregion
}
}
CustomerLead UpsertView :
<form method="post" enctype="multipart/form-data">
<div class="row p-3 border">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
@if (Model.CustomerLead.Id != 0)
{
//edit
title = "Edit Customer Lead";
<input type="hidden" asp-for="CustomerLead.Id" />
}
<div class="col-12 border-bottom">
<h2 class="text-primary">@title</h2>
</div>
<div class="col-8 pt-4">
<div class="form-group row border-primary">
<div class="col-4 mb-2">
<label asp-for="CustomerLead.Name"></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.Name" class="form-control" />
<span asp-validation-for="CustomerLead.Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row border-primary">
<div class="col-4 mb-2">
<label asp-for="CustomerLead.PhoneNumber"></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.PhoneNumber" type="number" class="form-control" />
<span asp-validation-for="CustomerLead.PhoneNumber" class="text-danger"></span>
</div>
</div>
<div class="form-group row mb-2">
<div class="col-4">
<label asp-for="CustomerLead.Email"></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.Email" class="form-control" />
<span asp-validation-for="CustomerLead.Email" class="text-danger"></span>
</div>
</div>
<div class="form-group row mb-2">
<div class="col-4">
<label asp-for="CustomerLead.Sector"></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.Sector" class="form-control" />
<span asp-validation-for="CustomerLead.Sector" class="text-danger"></span>
</div>
</div>
<div class="form-group row mb-2">
<div class="col-4">
<label asp-for="CustomerLead.TicketType"></label>
</div>
<div class="col-8">
One-Way
@Html.RadioButtonFor(model => model.CustomerLead.TicketType, "One Way")
Round-Trip
@Html.RadioButtonFor(model => model.CustomerLead.TicketType, "Round-Trip",new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group row">
<div class="col-4">
From
</div>
<div class="col-8">
@Html.DropDownListFor(m => m.CustomerLead.FromLocationsId, Model.FromLocationsList, "-Select a Location",
new { @class = "form-control" })
<span asp-validation-for="CustomerLead.FromLocationsId" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
To
</div>
<div class="col-8">
@Html.DropDownListFor(m => m.CustomerLead.ToLocationsId, Model.ToLocationsList,"-Select a Location",
new { @class = "form-control" })
<span asp-validation-for="CustomerLead.ToLocationsId" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
Lead Status
</div>
<div class="col-8">
@Html.DropDownListFor(m => m.CustomerLead.LeadStatus, Model.Leads,"-Select a Lead",
new { @class = "form-control" })
<span asp-validation-for="CustomerLead.LeadStatus" class="text-danger"></span>
</div>
</div>
<div class="form-group row mb-2">
<div class="col-4">
<label asp-for="CustomerLead.CreatedOn" hidden ></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.CreatedOn" hidden class="form-control" />
</div>
</div>
<div class="form-group row mb-2">
<div class="col-4">
<label asp-for="CustomerLead.CallBackTime"></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.CallBackTime " class="form-control" />
<span asp-validation-for="CustomerLead.CallBackTime" class="text-danger"></span>
</div>
</div>
<div class="form-group row mb-2">
<div class="col-4">
<label asp-for="CustomerLead.Comments"></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.Comments" class="form-control" />
</div>
</div>
@if (User.IsInRole(SD.Role_Lead_Data))
{
<div class="form-group row mb-2">
<div class="col-4">
<label asp-for="CustomerLead.CreatedBy"></label>
</div>
<div class="col-8">
<input asp-for="CustomerLead.CreatedBy" value=" @User.Identity.Name " readonly class="form-control" />
<span asp-validation-for="CustomerLead.CreatedBy" class="text-danger"></span>
</div>
</div>
}
<div class="form-group row">
<div class="col-8 offset-4">
@if (Model.CustomerLead.Id != 0)
{
<partial name="_EditAndBackToListButton" model="Model.CustomerLead.Id" />
}
else
{
<div class="row">
<div class="col">
<button type="submit" onclick="return validateInput()" class="btn btn-primary form-control">Create</button>
</div>
<div class="col">
<a asp-action="Index" class="btn btn-success form-control">Back to List</a>
</div>
</div>
}
</div>
</div>
</div>
</div>
</form>
This is my upsert view This is the picture of my upsert view
CustomerLead ViewModel:
public class CustomerLeadVm
{
public CustomerLead CustomerLead { get; set; }
public IEnumerable<LeadRemarks>? LeadRemarks { get; set; }
public IEnumerable<SelectListItem>? FromLocationsList { get; set; }
public IEnumerable<SelectListItem>? ToLocationsList { get; set; }
public IEnumerable<SelectListItem> Leads
{
get
{
return new[]
{
new SelectListItem {Text = "Initial" },
new SelectListItem {Text = "CallBack Requested" },
new SelectListItem {Text = "Canceled" },
new SelectListItem {Text = "Payment Approved" },
};
}
}
CustomerLeadcan have manyLeadRemarks?