how to display foreign key fields using asp.net mvc? - c#

Basically I wanted to display foreign key fields in asp.net cshtml. I have two tables roles and admin respectively. The foreign key relationship shows that for every admin there is a Role which has to be set.
I have done the part of adding role using role name. But when it comes to deleting or updating and such functionality I am not able to figure out the way to display the role name or other role details instead of roleId. I understand the concept of the doing, yet I do not get the right answer in asp.net mvc.
The following code for the cshtml, where I wanted to show the role name instead of role id.
#model IEnumerable<FastFactsTestPortalMVCApp.Models.Administrator>
#{
ViewBag.Title = "ViewAdmin";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<h2>ViewAdmin</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.AdminId)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminFirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminLastName)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminEmail)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminUserName)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminPassword)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminContactNumber)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminDesignation)
</th>
<th>
#Html.DisplayNameFor(model => model.AdminRoleId)
</th>
<th>
#Html.DisplayNameFor(model => model.CreatedAt)
</th>
<th>
#Html.DisplayNameFor(model => model.UpdatedAt)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.AdminId)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminFirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminLastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminEmail)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminUserName)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminPassword)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminContactNumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminDesignation)
</td>
<td>
#Html.DisplayFor(modelItem => item.AdminRoleId)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedAt)
</td>
<td>
#Html.DisplayFor(modelItem => item.UpdatedAt)
</td>
<td>
#Html.ActionLink("Update", "UpdateAdmin", item) |
#Html.ActionLink("Delete", "DeleteAdmin", item)
</td>
</tr>
}
</table>
The below code is for the the admin controller method - ViewAdmin:
public ActionResult ViewAdmin()
{
List<Models.Administrator> administrators = new List<Models.Administrator>();
FastFactsTestPortalMapper<Administrator, Models.Administrator> mapobj = new FastFactsTestPortalMapper<Administrator, Models.Administrator>();
FastFactsTestPortalRepository repObj = new FastFactsTestPortalRepository();
var lstEntityAdmins = repObj.GetAdministrators();
foreach(var admin in lstEntityAdmins)
{
administrators.Add(mapobj.Translate(admin));
}
return View(administrators);
}
Like wise when I update, the cshtml for the update functionality is :
#model FastFactsTestPortalMVCApp.Models.Administrator
#{
ViewBag.Title = "UpdateAdmin";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<h2 class="text-center">Update Admin</h2>
#using (Html.BeginForm("SaveUpdatedAdmin","Admin"))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Administrator</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.AdminId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminFirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminFirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminFirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminLastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminLastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminLastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminEmail, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminEmail, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminEmail, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminUserName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminUserName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminUserName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminPassword, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminPassword, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminPassword, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminContactNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminContactNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminContactNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminDesignation, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminDesignation, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminDesignation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AdminRoleId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AdminRoleId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AdminRoleId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CreatedAt, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CreatedAt, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CreatedAt, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UpdatedAt, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UpdatedAt, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.UpdatedAt, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "ViewAdmin")
</div>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
The controller method for the update functionality is
public ActionResult UpdateAdmin(Models.Administrator administrator)
{
var admindetails = administrator;
return View(administrator);
}
public ActionResult SaveUpdatedAdmin(Models.Administrator administrator)
{
if (ModelState.IsValid)
{
try
{
FastFactsTestPortalRepository repObj = new FastFactsTestPortalRepository();
FastFactsTestPortalMapper<Models.Administrator, Administrator> mapObj = new FastFactsTestPortalMapper<Models.Administrator, Administrator>();
var status = repObj.UpdateAdmin(mapObj.Translate(administrator));
if (status)
{
return RedirectToAction("ViewAdmin");
}
else
{
return View("Error");
}
}
catch (Exception)
{
return View("Error");
}
}
return View("UpdateAdmin", administrator);
}
The Output which I get is :
AdminId AdminFirstName AdminLastName AdminEmail AdminUserName AdminPassword AdminContactNumber AdminRoleId
1 Srinivas Muralidharan sm1043#gmail.com admin admin 09677299116 1
I want to change the AdminRoleId to RoleName : Administrator which is already in the database. Also I wanted to keep the change for all the other functionalities.
Kindly could you explain how to come over such problems?

Related

Table with Html.EditorFor

I am trying to create a form using a table. I have a total of 7 fields. What I am hoping to accomplish is have 3 fields on one row, 3 fields on the second row and the last row would be the last field extended. I tried using colspan however the editorfor box for NextAction is not extending all the way. There is a margin on the right hand side of this field. I've tried creating a CSS class to take away the margin however that did not work as well.
How can I get it so that the NextAction field extends all the way to the last column?
Below I've added my code for the last 2 rows of my table.
<table>
<tr>
<td>
#Html.LabelFor(model => model.Customer, htmlAttributes: new { #class = "control-label labelpadding" })
</td>
<td style="width:300px">
#Html.EditorFor(model => model.Customer, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer, "", new { #class = "text-danger" })
</td>
<td>
#Html.LabelFor(model => model.Principal, htmlAttributes: new { #class = "control-label col-md-2" })
</td>
<td style="width:300px">
#Html.EditorFor(model => model.Principal, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Principal, "", new { #class = "text-danger" })
</td>
<td>
#Html.LabelFor(model => model.Product, htmlAttributes: new { #class = "control-label labelpadding" })
</td>
<td>
#Html.EditorFor(model => model.Product, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product, "", new { #class = "text-danger" })
</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.Status, htmlAttributes: new { #class = "control-label labelpadding" })
</td>
<td>
#Html.DropDownListFor(m => m.Status, new SelectList(ViewBag.Status, "Status", "Text"), new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Status, "", new { #class = "text-danger" })
</td>
<td>
#Html.LabelFor(model => model.Value, htmlAttributes: new { #class = "control-label labelpadding" })
</td>
<td>
#Html.EditorFor(model => model.Value, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Value, "", new { #class = "text-danger" })
</td>
<td>
#Html.LabelFor(model => model.FollowUpDate, htmlAttributes: new { #class = "control-label labelpadding" })
</td>
<td>
<div>
<div class="input-group date" data-provide="datepicker">
#Html.EditorFor(model => model.FollowUpDate, new { htmlAttributes = new { #class = "form-control" } })
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.NextAction, htmlAttributes: new { #class = "control-label labelpadding" })
</td>
<td colspan="5">
#Html.EditorFor(model => model.NextAction, new { htmlAttributes = new { #class = "form-control ", #style = "margin-right: 0 !important;"} })
#Html.ValidationMessageFor(model => model.NextAction, "", new { #class = "text-danger" })
</td>
</tr>
</table>
Since you have Bootstrap enabled on your project, use Bootstrap's CSS class w-100. This class will make an element it is applied to have have 100% width. Here is the updated EditorFor line with that class added:
#Html.EditorFor(model => model.NextAction, new { htmlAttributes = new { #class = "form-control w-100"} })
If that doesn't work, you likely have something else overriding it. Check your other style sheets and the HTML that is rendered in your browser.

CRUD Edit can't retrieve data because of string id

I have a table called SectionUser like:
SectionUser
USER_ID (varchar(255))(Primary Key)
NAME (varchar(255))
Here are sample values:
USER ID | NAME
EVTAB | ELMER TABER
FAYKVIL | FAYK VILLET
I have a create action in controller that can create a section user successfully. My main problem is that when I edit. I can't get the value. So for example, I've entered wrong USER_ID and I want to edit it. The USER_ID is always null. This means that I want to edit my primary key.
Controller:
public ActionResult EditUser(string USER_ID)
{
if (USER_ID == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SectionUser section = db.SectionUsers.Find(USER_ID);
if (section == null)
{
return HttpNotFound();
}
return View(section);
}
View(cshtml)
#using (Html.BeginForm("EditUser", "Admin", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Section</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.USER_ID)
<div class="form-group">
#Html.LabelFor(model => model.USER_ID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.USER_ID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.USER_ID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.NAME, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.NAME, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.NAME, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
View(Index)
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.USER_ID)
</th>
<th>
#Html.DisplayNameFor(model => model.NAME)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.USER_ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
#Html.ActionLink("Edit", "EditUser", new { id = item.USER_ID.ToString().Trim() }) |
#Html.ActionLink("Details", "DetailsUser", new { id = item.USER_ID }) |
#Html.ActionLink("Delete", "DeleteUser", new { id = item.USER_ID })
</td>
</tr>
}
</table>
So my issue here is that the parameters I'm using on index.cshtml is wrong. This is the old code:
#Html.ActionLink("Edit", "EditUser", new { id = item.USER_ID.ToString().Trim() })
New Code:
#Html.ActionLink("Edit", "EditUser", new { USER_ID = item.USER_ID.ToString().Trim() })
As you can see in my EditUser Action, the parameter is string USER_ID.

ASP.Net MVC Viewmodel goes null to controller from View

I want to export my Viewmodel from my view to my controller which is receiving it in a method, this is my view:
#using Resources
#model MyProject.ViewModel.CertificationViewModel
#{
ViewBag.Title = Resources.titleCertificationList;
#section css
{
<link href="#Url.Content("~/layout/css/stylesformador.css")" rel="stylesheet" />
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
}
}
<div class="mainblock">
<div class="block-group">
<div class="ttitle title">#HIQResources.titleEdit</div>
</div>
#using (Html.BeginForm("Edit", "Certification", FormMethod.Post, new { id = "form1" }))
{
#Html.AntiForgeryToken()
<div class="md-content">
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.pdf, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#*<input type="file" name="file" />*#
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control inputForm", #type="file" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Code, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Code, new { htmlAttributes = new { #class = "form-control inputForm", #readonly = "readonly" } })
#Html.ValidationMessageFor(model => model.Code, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SelectedEntityId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.DropDownListFor(model => model.SelectedEntityId, Model.EntitiesDrop, htmlAttributes: new { #class = "form-control inputForm" })
#Html.HiddenFor(model => model.SelectedEntityId)
#Html.ValidationMessageFor(model => model.SelectedEntityId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.StatusId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.DropDownListFor(model => model.StatusId, Model.StatusDrop, htmlAttributes: new { #class = "form-control inputForm" })
#Html.HiddenFor(model => model.StatusId)
#Html.ValidationMessageFor(model => model.StatusId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Result, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Result, new { htmlAttributes = new { #class = "form-control inputForm" } })
#Html.ValidationMessageFor(model => model.Result, "", new { #class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Date, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control inputForm" } })
#Html.ValidationMessageFor(model => model.Date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Observation, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-8">
#Html.EditorFor(model => model.Observation, new { htmlAttributes = new { #class = "form-control inputForm" } })
#Html.ValidationMessageFor(model => model.Observation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-1">
<div class="col-md-8">
<table class="table table-bordered">
<tr>
<th class="col-md-2">
#Html.DisplayNameFor(model => model.FileName)
</th>
<th class="col-md-2">Ficheiro</th>
</tr>
#foreach (var item in Model.teste)
{
<tr>
<td>
#if (item.FileName == null)
{
<h5>Não existem ficheiros para esta certificação.</h5>
}
else
{
#Html.DisplayFor(modelItem => item.FileName)
}
</td>
<td>
#if (item.FileName == null)
{
<h5>Não existem ficheiros para esta certificação.</h5>
}
else
{
Download
#*Delete*#
<input type="submit" value="Edit" id="Delete" class="btn btn-default btn-sm btn-detail" />
}
#*#Html.ActionLink("Download", "Certification", "Download", new { id = Model.Id })*#
</td>
</tr>
}
</table>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-8">
<input type="submit" value="#HIQResources.buttonSave" class="btn btn-default btn-sm btn-detail" />
#Html.ActionLink(HIQResources.buttonBack, "CertificationList", new { id = Model.StudentId, nome = Model.StudentName }, new { #class = "btn btn-default btn-sm btn-edit" })
</div>
</div>
</div>
</div>
}
</div>
#section Scripts {
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/HIQTrainingScripts/Certification/Edit.js"></script>
<script>
$("#Date").datepicker({ dateFormat: 'yy/mm/dd' });
$("#Delete").click(function () {
var vm = $('#Model').serialize();
alert(vm)
var token = $('input[name="__RequestVerificationToken"]').val();
$.ajax({
url: "/Certification/Edit",
type: "POST",
data: {
__RequestVerificationToken: token,
vm: vm,
click: "deu",
},
sucess: function (result) {
alert("sucess");
}
});
});
</script>
}
This is the method in my controller:
[Authorize(Roles = "Admin, Staff")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(CertificationViewModel vm, string click) {
if (click == "deu")
{
int lol = _servicesFactory.GetService<ICertificationManager>().DeleteFile(vm, GetLoggedUser());
return View(vm);
}
}
The value of my ViewModel comes always as null to my Jquery variable and I don't know why, because it should come with the info from the view to the controller in order for me, to execute my task.
Am I missing something ?

MVC5 - View does not return the Id of my object on post

My view is shown below:
#model KtembRegistry.Models.Entities.Member
#{
ViewBag.Title = "Üye Güncelleme";
}
<h2>#ViewBag.Title</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.MemberId)
#Html.HiddenFor(model => model.Director.EmployeeId);
<div class="form-horizontal">
<hr />
<div class="form-group">
<input type="button" value="Back to List" onclick="location.href='#Url.Action("Index", "Members")'" class="btn btn-primary" />
<input type="submit" value="Save" class="btn btn-success" />
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.MemberName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.MemberName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.MemberName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Location, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Location, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Location, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.MemberClass, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.MemberClass, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.MemberClass, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ClassId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ClassId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ClassId, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.LastApprovalDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastApprovalDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastApprovalDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TaxNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TaxNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TaxNo, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Phone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Fax, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Fax, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Fax, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Mobile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mobile, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Address, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h4>Direktör</h4>
<hr />
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Director.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Director.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.LastName, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Director.IdentityNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.IdentityNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.IdentityNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Director.Responsibility, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.Responsibility, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.Responsibility, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h3> Personel </h3>
<hr />
<table class="table table-hover">
<thead>
<tr>
<th>İsim</th>
<th>Kimlik No</th>
<th>Yetki</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Employees)
{
<tr>
<td>#item.FirstName</td>
<td>#item.IdentityNo</td>
<td>#item.Responsibility</td>
<td>
#Html.ActionLink("Edit", "Edit", new { #item.EmployeeId }) |
#Html.ActionLink("Delete", "Delete", new { #item.EmployeeId })
</td>
</tr>
}
</tbody>
</table>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Entity:
public class Employee
{
public virtual int Id { get; protected set; }
public virtual int EmployeeNo { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int IdentityNo { get; set; }
public virtual string Responsibility { get; set; }
public virtual Member Company { get; set; }
}
When I do post, everything is sent to controller just fine except model.Director.EmployeeId. The controller sends the EmployeeId field correctly to the field. Whatever, I do it does not work. Any ideas why my post does not work ?
I have an old habit of making the setter of identity fields in my entities protected in order to protect outside mangling. However, in this case it was causing the identity field from being updated which was the cause of the problem.

Stampings Algorithm in ASP.NET MVC LINQ Entity Framework

As the title says, I'am trying to write an algoritm with these conditions:
Each employee has a flextime balance which is initially zero.
If the total time of all stamp ins and stamp outs in a working day exceeds 8 hours, the flexitime balance listed with the prolonged time , and the same principle applies to the deficit.
Flextime balance is listed with all the time stamped on the days that are relieved of work.
If no stamps occurred on a working day then the flextime balance is not affected.
There are two tables im having in the database: NonWorkingDays and Stampings.
NonWorkingDays contains only of one column which is: "Days (DateTime)".
Stampings contains of four columns: "Id (Int) (PK), UserId (Int), Timestamp (DateTime), StampingType (value "in" and value "out")
This is what I wrote so far -
Model:
public class FlexModel
{
public List<User> Users { get; set; }
public List<Stamping> Stampings { get; set; }
public decimal FlexTime { get; set; }
}
View:
#model Aviato.ViewModel.FlexModel
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h2>Info</h2>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Users[0].UserId)
#Html.HiddenFor(model => model.Users[0].SocialSecurityNumber)
#Html.HiddenFor(model => model.Users[0].FirstName)
#Html.HiddenFor(model => model.Users[0].LastName)
#Html.HiddenFor(model => model.Users[0].EmploymentStartDate)
#Html.HiddenFor(model => model.Users[0].EmploymentEndDate)
#Html.HiddenFor(model => model.Users[0].Password)
#Html.HiddenFor(model => model.Users[0].RoleName)
<div class="form-group">
#Html.LabelFor(model => model.Users[0].Address1, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Users[0].Address1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Users[0].Address1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Users[0].Address2, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Users[0].Address2, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Users[0].Address2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Users[0].ZipCode, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Users[0].ZipCode, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Users[0].ZipCode, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Users[0].City, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Users[0].City, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Users[0].City, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Users[0].PhoneNumber1, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Users[0].PhoneNumber1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Users[0].PhoneNumber1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Users[0].PhoneNumber2, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Users[0].PhoneNumber2, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Users[0].PhoneNumber2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FlexTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.FlexTime, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FlexTime, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Spara" class="btn btn-default" />
</div>
</div>
</div>
}
Controller:
public ActionResult Info()
{
var flexModel = new FlexModel();
var userId = (int)Session["userId"];
var user = _db.Users.Find(userId);
var stampIn = _db.Stampings.Where(i => i.StampingType == "in").Where(i => i.User == user).ToList();
var stampOut = _db.Stampings.Where(i => i.StampingType == "out").Where(i => i.User == user).ToList();
var workDay = 8;
if (stampIn.Count == 0)
{
return View();
}
foreach (var itemIn in stampIn)
{
}
foreach (var itemOut in stampOut)
{
}
return View();
}
[HttpPost]
public ActionResult Info(FlexModel model)
{
if (ModelState.IsValid)
{
foreach (var item in model.Users)
{
_db.Entry(item).State = EntityState.Modified;
}
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
So this is not working for me, I get the value of Flex to display 0.
Help would be appreciated!

Categories