1
I am new to programming and need to add some functionality in a C# Asp.net. One of them is a control that prevents the user from proceeding with the request if he has any pending in the system. I will describe the step by step.
My view
@using Integracao.ImpressoRequests.Model.Entities.ImpressoRequest
@model Request
@{
ViewBag.Title = "Requisição";
Layout = "~/Views/Shared/_Layout.cshtml";
}
...
@foreach (RequestItem item in Model.Items)
{
<tr>
<td>
@Html.DisplayFor(x => item.Usuario.Nome)
</td>
<td>
@Html.DisplayFor(x => item.Date)
</td>
<td>
@Html.DisplayFor(x => item.Unidade.Nome)
</td>
<td>
@Html.DisplayFor(x => item.ResultCenter.Nome)
</td>
</tr>
}
</table>
<div class="formfooter">
<input type="button" value="Gerar pedido" onclick="window.open('@Url.Action("ProcessRequest", new { requestId = Model.Id })','_blank');window.location.href='@Url.Action("Index", "Home")'" /></div>
Controller
[HttpGet]
public ActionResult ProcessRequest(int requestId)
{
Request request = _data.Requests.GetByID(requestId);
if (request.Processed)
return RedirectToAction("PrintRequest", new { requestId = request });
foreach (RequestItem item in request.Items.OrderBy(x => x.Usuario_Id))
{
AssignImpressos assignIItems = new AssignImpressos(item.Id);
assignIItems.Assign();
}
request.Processed = true;
_data.Save();
return RedirectToAction("PrintRequest", new { requestId = request.Id });
}
Model
public Request()
{
Date = DateTime.Now;
UsedItems = new List<Impresso>();
RequestsHistory = new List<RequestHistory>();
Items = new List<RequestItem>();
}
public int Id { get; set; }
public DateTime? Date { get; set; }
public string Justification { get; set; }
public string Usuario_Id { get; set; }
public virtual Usuario Usuario { get; set; }
public virtual ICollection<Impresso> UsedItems { get; set; }
Model Impresso
public class Impresso : IEntity
{
public Impresso()
{
RequestsHistory = new List<RequestHistory>();
}
public int Id { get; set; }
public int Code { get; set; }
public string Purpose { get; set; }
public DateTime? Data_Controle { get; set; }
public decimal AdjustedValue { get; set; }
public int Status { get; set; }
public ItemStatus EnumeratedStatus
{
get { return (ItemStatus)Status; }
set { Status = (int)value; }
}
public string Note { get; set; }
public string Usuario_Id { get; set; }
public virtual Usuario Usuario { get; set; }
public int? Request_Id { get; set; }
public virtual Request Request { get; set; }
public string Unidade_Id { get; set; }
public virtual Unidade Unidade { get; set; }
public string ResultCenter_Id { get; set; }
public virtual ResultCenter ResultCenter { get; set; }
public virtual ICollection<RequestHistory> RequestsHistory { get; set; }
object IEntity.Id
{
get { return Id; }
set { Id = (int)value; }
}
}
What I need is the following: For the user to click 'Generate Request' in the view, in my controller class I need to scan the Request table in the database looking if that user already authenticated by the system has in the Printed table the field 'Data_control' null or the Status = 1. If positive, the system shall prevent the continuation by triggering an alert with the information. If false, the program continues and generates the request normally. The question is: I know how to do a select in the database to get it but what is the syntax in c# according to my code to do this control? I thought in query because I work with database and the logic is clearer to me, or if not by query, what would be the best way to achieve this in programming logic anyway? Thank you
Edit
private string GetUser()
{
string userId = string.Empty;
try
{
string id = User.Identity.Name.Split('\\')[1];
Usuario _currentUsuario = _data.Usuario.GetByID(id);
userId = _currentUsuario.Id;
ViewBag.UserName = _currentUsuario.Name;
}
catch (Exception ex)
{
ModelState.AddModelError("UserName", ex.Message);
}
return userId;
}
You want that warning to appear on the screen as?
– Leonel Sanches da Silva
@Ciganomorrisonmendez can be a simple Alert or messagebox... The important thing is to lock the user and go back to the home screen when he gives the ok of aware of not being able to request...
– PFVictor