I am having trouble with this last part of my project. I have all of my SQL Table columns filled up except for this last part. I have two tables WorkSchedule and WorkShiftBid.
In WorkShiftBid, I have WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID
I will be using Guid.NewGuid() for WorkShiftBidID, Datetime.now for WSBidDatetime, A hardcoded "pending" value for WSBidStatus, A viewbag for StaffID and I am trying to get the WorkScheduleID to work but to no avail.
For WorkSchedule, I am trying to get just the "WorkScheduleID" attribute just by using a button method asp-route-id="#item.WorkScheduleID" and it will redirect me to the Create page whereby I will select a Staff from the drop-down list and then save it to my WorkShiftBid table as it has a foreign key for "WorkScheduleID".
It would be helpful if someone could help me. Thank you.
Controller Code:
public IActionResult CreateWorkShift()
{
ViewData["StaffID"] = new SelectList(_context.Staff, "StaffID", "StaffName");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateWorkShift(Guid? id, [Bind("WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID")] WorkShiftBidModel workShiftBidModel)
{
if (id != workShiftBidModel.WorkScheduleID)
{
return NotFound();
}
if (ModelState.IsValid)
{
workShiftBidModel.WSBidDateTime = DateTime.Now;
workShiftBidModel.WorkShiftBidID = Guid.NewGuid();
_context.Add(workShiftBidModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(DisplaySchedule));
}
ViewData["StaffID"] = new SelectList(_context.Staff, "StaffID", "StaffName", workShiftBidModel.StaffID);
return View(workShiftBidModel);
}
private bool WorkShiftBidModelExist(Guid id)
{
return _context.WorkSchedule.Any(e => e.WorkScheduleID == id);
}
Display WorkShift Page:
<h1 class="text-center"> Apply Workshifts</h1>
<hr />
<table class="table">
<thead>
<tr>
<th>
From (DateTime)
</th>
<th>
To (DateTime)
</th>
<th>
Day
</th>
<th>
Status
</th>
<th>
Work Descriptions
</th>
<th>
Branch
</th>
<th>
Manager
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleFromDateTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleToDateTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleFromDateTime.DayOfWeek)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleStatus)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkDescriptions.WorkDescriptionName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Branches.BranchName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Staff.StaffName)
</td>
<td>
<form method="post">
<input type="submit" class="btn btn-primary btn-block" value="Bid" asp-route-id="#item.WorkScheduleID" asp-action="DisplaySchedule" asp-controller="PartTimer" />
</form>
<div class="form-group col-md-6">
<a asp-action="CreateWorkShift" asp-route-id="#item.WorkScheduleID" class="btn btn-success">Create</a>
</div>
</td>
</tr>
}
</tbody>
</table>
Create view page code:
<h1>Create</h1>
<h4>Work Shifts</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="CreateWorkShift">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="WorkScheduleID"/>
<div class="form-group">
<label asp-for="WSBidStatus" class="control-label"></label>
<input readonly asp-for="WSBidStatus" value="Pending" class="form-control"/>
<span asp-validation-for="WSBidStatus" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="StaffID" class="control-label"></label>
<select asp-for="StaffID" class="form-control" asp-items="ViewBag.StaffID"></select>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="submit" value="Save" class="btn btn-primary btn-block" />
</div>
<div class="form-group col-md-6">
<a asp-action="ProfilePage" class="btn btn-secondary btn-block"><i class=" fa fa-table"></i>Back to List</a>
</div>
</div>
</form>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Do you mean you cannot get the WorkScheduleID in public async Task<IActionResult> CreateWorkShift(Guid? id, [Bind("WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID")] WorkShiftBidModel workShiftBidModel).If so,you need to get and pass Id in public IActionResult CreateWorkShift.Here is a demo worked:
public IActionResult CreateWorkShift(int id)
{
List<Staff> list = new List<Staff> { new Staff { StaffID = 1, StaffName = "staff1" }, new Staff { StaffID = 2, StaffName = "staff2" } };
ViewData["StaffID"] = new SelectList(list, "StaffID", "StaffName");
WorkShiftBidModel w = new WorkShiftBidModel { WorkScheduleID = id };
return View(w);
}
result:
Related
I create a view and create two cards to show new skill and current skill.
The problem is TittleNewSkillEHS back to the controller cero data after I select a new skill.
Short description of my request
I will need to display two lists with information about New Skill (required checkbox to select and add current skill), Current skill (required update target).
One approached to solution the request I create a view model take EmployeeTitle id, Title name, and two list
• List Current skills
• List New skills
.
.
.
TitleSkillVM TitleSkillVMNew = new TitleSkillVM();
TitleSkillVMNew.TitleId = SelectTitle.TitleId;
TitleSkillVMNew.TitleDescription = SelectTitle.TitleName;
TitleSkillVMNew.TittleNewSkillEHS = _tempNewEHS; “New skill”
TitleSkillVMNew.TittleSkillEHS = _tempEHS; “List Current skill”
return View(TitleSkillVMNew);
}
#model WCMApps.Models.ViewModel.TitleSkillVM
<div class="row">
<div class="col-sm-6">
<form asp-controller="AdmTitles" asp-action="InsertNewSkillTarget" method="post">
<input type="hidden" asp-for="TitleId" />
<%: Html.HiddenIndexerInputForModel() %>
<input type="hidden" asp-for="TittleNewSkillEHS" />
<div class=" card">
<div class="card-header">
<div class="container">
<div class="row">
<div class="col-9">
<h4>New Skill Index</h4>
</div>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th></th>
<th scope="col">
Skill Name
</th>
<th scope="col">
Current Target
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.TittleNewSkillEHS)
{
if (item.TitleSkillTargetStatus == true)
{
<tr class="table-info">
<td>
#Html.CheckBoxFor(modelItem => item.IsSelectedNewSkillTitle, item.TitleSkillTargetId)
#Html.HiddenFor(modelItem => item.TitleSkillTargetId)
</td>
<td>
#Html.DisplayFor(modelItem => item.SkillLevelDDescription)
#Html.HiddenFor(modelItem => item.SkillLevelDDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.Target)
#Html.HiddenFor(modelItem => item.Target)
</td>
</tr>
}
}
</tbody>
</table>
</div>
<div class="card-footer">
<small class="text-muted">
<button type="submit" class="btn btn-outline-info" onclick="location.href='#Url.Action("InsertNewSkillTarget", "AdmTitles", Model.TittleNewSkillEHS)'">Save</button>
</small>
</div>
</div>
</form>
</div>
<div class="col-sm-6">
<form asp-controller="AdmTitles" asp-action="UpdateCurrentSkillTarget" method="post">
<div class="card">
<div class="card-header">
<div class="container">
<div class="row">
<div class="col-9">
<h4>Current Skill Index</h4>
</div>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">
Skill Name
</th>
<th scope="col">
Current Target
</th>
<th scope="col">
Target
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.TittleSkillEHS)
{
if (item.TitleSkillTargetStatus == true)
{
<tr class="table-success">
<td>
#Html.DisplayFor(modelItem => item.SkillLevelDDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.Target)
</td>
<td>
<div class="form-group">
<input asp-for="Target" class="form-control" />
<span asp-validation-for="Target" class="text-danger"></span>
</div>
</td>
</tr>
}
}
</tbody>
</table>
</div>
<div class="card-footer">
<small class="text-muted">
<button type="button" class="btn btn-outline-info" onclick="location.href='#Url.Action("UpdateCurrentSkillTarget", "AdmTitles", Model.TittleSkillEHS)'">Update</button>
</small>
</div>
</div>
</form>
</div>
</div>'
Here is a demo to pass TittleNewSkillEHS to Controller:
View:
<div class="card-header">
<div class="container">
<div class="row">
<div class="col-9">
<h4>New Skill Index</h4>
</div>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th></th>
<th scope="col">
Skill Name
</th>
<th scope="col">
Current Target
</th>
</tr>
</thead>
<tbody>
#{ var count = 0;}
#foreach (var item in Model.TittleNewSkillEHS)
{
if (item.TitleSkillTargetStatus == true)
{
<tr class="table-info">
<td>
#*#Html.CheckBoxFor(modelItem => item.IsSelectedNewSkillTitle, new { #name = "TittleNewSkillEHS[#count].IsSelectedNewSkillTitle" })
#Html.CheckBox("IsChecked", new { #name = "TittleNewSkillEHS[#count].IsSelectedNewSkillTitle" })*#
<input asp-for=#item.IsSelectedNewSkillTitle name="TittleNewSkillEHS[#count].IsSelectedNewSkillTitle"/>
<input name="TittleNewSkillEHS[#count].TitleSkillTargetId" value=#item.TitleSkillTargetId hidden />
</td>
<td>
#Html.DisplayFor(modelItem => item.SkillLevelDDescription)
<input name="TittleNewSkillEHS[#count].SkillLevelDDescription" value=#item.SkillLevelDDescription hidden />
#*#Html.HiddenFor(modelItem => item.SkillLevelDDescription)*#
</td>
<td>
#Html.DisplayFor(modelItem => item.Target)
<input name="TittleNewSkillEHS[#count].Target" value=#item.Target hidden />
#*#Html.HiddenFor(modelItem => item.Target)*#
</td>
</tr>
count++;
}
}
</tbody>
</table>
</div>
<div class="card-footer">
<small class="text-muted">
<button type="submit" class="btn btn-outline-info">Save</button>
</small>
</div>
</div>
Controller:
public IActionResult InsertNewSkillTarget(TitleSkillVM t) {
return Ok();
}
result:
How would I do to when clicking the checkBox I call the action "ShowActives" of the controller?
This controller returns a list of elements that are active in the database.
How would I return these elements in the table?
my index is my default controller, clicking the checkBox I would like to call the ShowActives Action
public class CodigosDeOperacaoController : BaseController
{
public ActionResult Index()
{
return View(_codigosOperacionaisService.GetAll());
}
public ActionResult ShowActives ()
{
return View(_codigosOperacionaisService.GetActives());
}
}
My View
<div class="row">
<div class="col-md-12">
#using (Html.BeginForm("Index", "CodigosDeOperacao", FormMethod.Get))
{
<div class="row">
<div id="custom-search-input">
<div class="input-group col-md-12">
#Html.TextBox("buscar", null, new { #class = "form-control input", #placeholder = "Pesquisar por nome" })
<span class="input-group-btn">
<button class="btn btn-info btn" type="submit">
<big><i class="fa fa-search-plus"></i></big>
</button>
</span>
</div>
</div>
<div class="col-md-3 text-right">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Mostrar Todos</label>
</div>
<div class="col-md-6 text-right ">
<div>
<button type="button" id="btnNovo" class="btn btn-outline-primary btn-sm" style="min-width: 200px;">Novo código</button>
</div>
</div>
</div>
}
</div>
</div>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Codigo)
</th>
<th>
#Html.DisplayNameFor(model => model.Descricao)
</th>
<th>Ações</th>
</tr>
#foreach (var item in Model)
{
if (!item.Ativo == false)
{
<tr style="background-color:aliceblue">
<td>
#Html.DisplayFor(modelItem => item.Codigo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Descricao)
</td>
<td>
<a href="#Url.Action("Edit", "CodigosDeOperacao", new { id=item.Id })" class="btn btn-warning" style="margin-bottom: 3px" id="tamanho-botoes">
<span class="fa fa-edit"></span>
</a>|
<a href="#" class="btn btn-danger" style="margin-bottom: 3px" id="tamanho-botoes" onclick="iniciarExclusao('#item.Id')">
<span class="fa fa-ban"></span>
</a>
</td>
</table>
}
You can use Url.Action. Something like this should work:
<input type="checkbox" class="form-check-input" id="exampleCheck1"
onclick="location.href='<%: Url.Action("ShowActives", "CodigosDeOperacaoController") %>'" >
I have a table on my view and I pass a model to this view on tbody I get values from the model and a select where I get values from a viewbag. I want to relate the values that are on each row something like Metadisciplina.Nome with the selected value on ViewBag.Docente.... Per each row I will get something like a list I don't know. Right now I have a method where I get two different types of lists where in one I get the model and in the other the values from the select option... But using this two cycles and a lot of if's I just can have one of my ideas... Or I can get for the same Docente value each Metadisciplina.Nome or for each Metadisciplina.Nome get different Docente values... I want to have the possibilty to have both Metadisciplina.Nome with different Docente values and Metadisciplina.Nome with the same Docente values.. The user will decide that...
View
#model IList<ModelsLibrary.Turno>
#{
ViewData["Title"] = "Index";
}
<div class="row">
<div class="col-md-4">
<form asp-action="Index" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<div class="form-group">
<label>Ano Letivo</label>
<select name="anoLetivo" class="form-control" asp-items="ViewBag.Ano"></select>
</div>
<div class="form-group">
<label>Ano Curricular</label>
<select name="ano" class="form-control">
<option>-Select-</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
<div class="form-group">
<label>Semestre</label>
<select name="semestre" class="form-control">
<option>-Select-</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div class="form-group">
<input type="submit" value="Escolhido" class="btn btn-default" />
</div>
</form>
</div>
</div>
<form asp-action="Create" method="post">
<table class="table">
<thead>
<tr>
<th>
<label>Disciplina</label>
</th>
<th>
<label>Turno</label>
</th>
<th>
<label>Tipologia</label>
</th>
<th>
<label>Docente</label>
</th>
</tr>
</thead>
<tbody>
#for (var i = 0; i < Model.Count; i++)
{
#Html.HiddenFor(x => x[i].TurnoId)
#Html.HiddenFor(x => x[i].MetaDisciplina.Nome)
<tr>
<td>
#Html.DisplayFor(modelItem => modelItem[i].MetaDisciplina.Nome)
</td>
<td>
#Html.DisplayFor(modelItem => modelItem[i].LetraTurno)
</td>
<td>
#Html.DisplayFor(modelItem => modelItem[i].Tipologia.Tipo)
</td>
<td>
<select name="DocenteId" asp-items="ViewBag.Docente"></select>
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Controller Method
[HttpPost]
public async Task<IActionResult> Create(IList<Turno> Turno, IList<int> DocenteId)
{
var turnodocente = new List<TurnoDocente>();
foreach (var item in Turno)
{
foreach (var ite in DocenteId)
{
if(!turnodocente.Any(x=>x.DocenteId == ite && x.TurnoId == item.TurnoId))
{
turnodocente.Add(new TurnoDocente
{
TurnoId = item.TurnoId,
DocenteId = ite
});
}
}
}
HttpResponseMessage responsemeta = await HttpRequestBuilder.WebApiClient.PostAsJsonAsync("api/TurnoDocente", turnodocente);
responsemeta.EnsureSuccessStatusCode();
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
Expected Result
I expect to get the values without the red cross not all.... This is one of the options.. In here I just have an example but I try another way to do the if clause but the result is not what I want... And and didn't really know how I can get the values that are related per each table row...
I want to implement a bulk operation function, but I cannot post my data to the razor page code behind, anyone can help? Please check my code below
<form method="post" asp-page="./Index">
<div class="row">
<div class="col-md-12">
<table class="table table-hover table-bordered" style="margin-top:15px;">
<thead>
<tr class="info">
<th></th>
<th>
<a asp-page="./Index" asp-route-sortOrder="#Model.NumberSort">
#Html.DisplayNameFor(model => model.Products[0].Number)
</a>
</th>
<th>
#Html.DisplayNameFor(model => model.Products[0].Name)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Products)
{
<tr>
<td>
#Html.CheckBoxFor(modelItem => item.Checked)
</td>
<td class="success">
#Html.DisplayFor(modelItem => item.Number)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a asp-page="./Details" asp-route-id="#item.ID" class="btn btn-info">details</a>
<a asp-page="./Edit" asp-route-id="#item.ID" class="btn btn-warning">edit</a>
<a asp-page="./Delete" asp-route-id="#item.ID" class="btn btn-danger">delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-md-10">
#{
var items = Model.Products.Where(x => x.Checked);
}
<input type="submit" name="bulkUpload" value="bulk upload" asp-route-data="#items"/>
</div>
</div>
</form>
public async Task OnPostAsync(List<Product> upload)
{
}
You need to render your products in such way that the Modelbinder will be able to pick them up on form submit and pass them into the codebehind.
Something like this (done by hand so it might not compile at first):
#{
for (int i = 0; i < Model.Products.Count; i++)
{
<input type="checkbox" asp-for="Model.Produtcs[i].Checked" />
#Model.Products[i].Number
#Model.Products[i].Name
}
}
This will render HTML like
<input type="checkbox" name="Products[0].Checked" />
<input type="checkbox" name="Products[1].Checked" />
<input type="checkbox" name="Products[2].Checked" />
And it should get back to your OnPostAsync. If not then try changing
public async Task OnPostAsync(List<Product> upload)
to
public async Task OnPostAsync(YourModel model)
What ever your full model is named.
This is then redundant:
#{
var items = Model.Products.Where(x => x.Checked);
}
Here is Controller Code
//Approved
public ActionResult Approved(String VoucherNIK, String Admin, int? month, int? Year, int? minPrice, int? maxPrice)
{
if (Session["Nama_Lengkap"] != null)
{
var NIKLst = new List<int>();
var NIKQry = from d in db.Voucher
where d.Voucher_Number >= 2000
orderby d.NIK
select d.NIK;
var query = from app in db.Approved
join det in db.Detail on app.ID_Approved equals det.ID_Approve
join vou in db.Voucher on det.ID_Voucher equals vou.ID_Voucher
join adm in db.Admin on app.ID_Admin equals adm.ID_Admin
select new
{
vou.NIK,
app.Tanggal_Approve,
app.Total,
app.ID_Admin,
adm.Nama_Lengkap,
app.ID_Approved
};
IEnumerable < int > enumerable = NIKQry.GroupBy(v => v).Select(group => group.FirstOrDefault());
NIKLst = enumerable.ToList();
ViewBag.VoucherNIK = new SelectList(NIKLst);
try
{
if (!string.IsNullOrEmpty(Admin))
{
query = query.Where(s => s.Nama_Lengkap.Contains(Admin));
}
}
catch (DataException /* dex */)
{
ModelState.AddModelError("", "Unable to Apply.");
}
return View(query));
}
else
{
return RedirectToAction("Login", "Account");
}
}
And the View
#model IEnumerable<Voucher.Models.Approved>
#{
ViewBag.Title = "Approved";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
#using (Html.BeginForm("Approved", "Voucher", FormMethod.Get))
{
<div class="form-horizontal">
<hr />
<div class="form-group">
NIK Voucher :
<div class="col-md-10">
#Html.DropDownList("VoucherNIK", null, "All") <!-- new { onchange = "document.location.href = '?VoucherNIK=' + this.value;" })!-->
</div>
</div>
<div class="form-group">
ADMIN Approve :
<div class="col-md-10">
#Html.TextBox("Admin")
</div>
</div>
<div class="form-group">
Month :
<div class="col-md-10">
#Html.TextBox("month")
</div>
</div>
<div class="form-group">
Year :
<div class="col-md-10">
#Html.TextBox("Year")
</div>
</div>
<div class="form-group">
Price(min NUMBER) :
<div class="col-md-10">
#Html.TextBox("minPrice")
</div>
</div>
<div class="form-group">
Price(max NUMBER) :
<div class="col-md-10">
#Html.TextBox("maxPrice")
</div>
</div>
</div>
<input type="submit" value="Filter" class="btn-toolbar" /><br />
}
<br />
<table class="table">
<tr>
<th>
Nama Admin
</th>
<th>
ID Approved
</th>
<th>
Tanggal
</th>
<th>
ID Admin
</th>
<th>
TOTAL
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Nama_Lengkap)
</td>
<td>
#Html.DisplayFor(modelItem => item.ID_Approved)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tanggal_Approve)
</td>
<td>
#Html.DisplayFor(modelItem => item.ID_Admin)
</td>
<td>
#Html.DisplayFor(modelItem => item.Total)
</td>
<td>
#Html.ActionLink("More Detail", "DetailAppr", new { id = item.ID_Approved })
</td>
</tr>
}
</table>
When Run i got this error
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1
,but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcMarks.Models.MarksType]'
Change return statement to
return View(query
.Select(it => new MarksType{
//initilize properties of `MarksType` with `it`
}));
Your view expects a model of type IEnumerable<MarksType> but query variable is IEnumerable of Anonymous objects.