In some of my tables in the Index View, some roles can see more of the table than others.
So this means I should hide some columns
I've done this quick and dirty as such:
<% bool codesZichtbaar = Roles.IsUserInRole("Admin") || Roles.IsUserInRole("Algemeen Beheer") || Roles.IsUserInRole("Secretariaat"); %>
<table>
<tr>
<th>
</th>
<th>
Adres
</th>
<th>
Instellingsnr.
</th>
<% if (codesZichtbaar) { %>
<th>
Codes
</th>
<%} %>
<th>
IKON
</th>
<th>
Datum Van-Tot
</th>
<th>
</th>
</tr>
<% foreach (var item in Model) { %>
<tr class="inst<%: item.actieveSchool?"active":"inactive" %>">
<td>
<% Html.RenderPartial("buttons", new MVC2_NASTEST.Models.ButtonDetail() { RouteValues = new { id = item.Inst_ID }, edit = false, details = true, delete = false, takeOptionalsFromUrl = true }); %>
</td>
<td>
<%: item.INST_NAAM%><br />
<%: item.STRAAT%><br />
<%: item.POSTCODE%>
<%: item.GEMEENTE%>
</td>
<td>
<%: item.DOSSNR%>
</td>
<% if (codesZichtbaar) { %>
<td>
Gebruiker:
<%: item.Inst_Gebruikersnaam%><br />
C:
<%: item.Inst_Concode%><br />
D:
<%: item.Inst_DirCode%>
</td>
<%} %>
<td>
T:
<%: item.INST_TYPE%><br />
Inst_REF:
<%: item.INST_REF%><br />
Loc_REF:
<%: item.INST_LOC_REF%><br />
Loc_Nr:
<%: item.INST_LOCNR%>
</td>
<td>
<%: String.Format("{0:d}", item.DATUM_VAN)%>
-
<%: String.Format("{0:d}", item.DATUM_TOT)%>
</td>
<td>
<a href='<%= Url.Action("Links", "Instelling", MVC2_NASTEST.RouteValues.MergeRouteValues(MVC2_NASTEST.RouteValues.optionalParamters(Request.QueryString), new {id=item.Inst_ID})) %>'
title="Linken">
<img src="<%= Url.Content("~/img/link.png") %>" alt="Link" width="16" /></a>
</td>
</tr>
<% } %>
</table>
but this is MVC, my code is dry, what is the correct way to do this? or, what is a cleaner way to do this.
I would write a Html helper for this. It could look something like this:
public bool IsInRole(this HtmlHelper instance, params string[] roles)
{
var user = html.ViewContext.HttpContext.User;
foreach(var role in roles)
{
if(user.IsInRole(role))
return true;
}
return false;
}
This function will return true if user is in any of the provided roles. You would use it like this:
<tr>
<td>First - Visible to all</td>
<% if(Html.IsInRole("Admin", "Algemeen Beheer", "Secretariaat")) { %>
<td>Only for privileged users</td>
<% } %>
<td>Last - Visible to all
</tr>
This is a light, clean and reusable way of doing it.
Related
I have the following partial view:
#model IDECOHealthInsurance.Models.Pharmacy
#using (Ajax.BeginForm("pharmacyDetials", "Pharmacy", new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "pDetail", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, LoadingElementId = "Loading", OnBegin = "" }))
{
<h4>تفاصيل الصيدلية</h4>
<div id="pDetail" class="MainGridContainer pb-5">
#if (Model.dtItemsDetails != null)
{
<table dir="rtl" id="Paitents" class="MainGrid">
<thead>
<tr style="text-size-adjust:auto">
<th>
رقم الموظف
</th>
<th>
التاريخ
</th>
<th>
الوقت
</th>
<th>
المستفيدون
</th>
<th>
ملاحظات
</th>
<th>
الباركورد
</th>
<th>
أسم العينة
</th>
<th>
الكمية
</th>
<th>
السعر
</th>
</tr>
</thead>
<tbody>
#foreach (System.Data.DataRow row in Model.dtItemsDetails.Rows)
{
<tr style="width:100%">
<td>
#row["EMPLOYEE_NUMBER"]
</td>
<td>
#row["ENTRY_DATE"]
</td>
<td>
#row["ENTRY_TIME"]
</td>
<td>
#row["BENEFICIARIES"]
</td>
<td>
#row["NOTE"]
</td>
<td>
#row["ITEM_CODE"]
</td>
<td>
#row["ITEM_NAME"]
</td>
<td>
#row["QTY"]
</td>
<td>
#row["PRICE"]
</td>
</tr>
}
</tbody>
</table>
}
</div>
}
And the follwing controller:
[HttpGet]
public ActionResult pharmacyDetials(Pharmacy model)
{
var masterID = Convert.ToInt32(Session["login"]);
if (masterID == 0)
{
return RedirectToAction("Login");
}
else
{
Models.Pharmacy objPharamcyMode = new Pharmacy();
IDECOServiceReference.IdecoAPIServiceClient idecoAPI = new IDECOServiceReference.IdecoAPIServiceClient();
DataTable dataTable = idecoAPI.GETPHARMACYEMPLOYEEMASTER("", 1);
model.dtItemsDetails = dataTable;
return PartialView("_PharmacyDetails", model);
}
}
And the following main view:
#model IDECOHealthInsurance.Models.Pharmacy
#{
ViewBag.Title = "PharmacyApplication";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table style="height:680px; width:1280px; border:hidden">
<tr>
<td>
<div id="pDetail">
#Html.Partial("_PharmacyDetails", Model)
</div>
</td>
<td>
#using (Ajax.BeginForm("PharmacyApplication", "Pharmacy", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "updatePnl", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, LoadingElementId = "Loading", OnBegin = "" }))
{
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="panel" value="أضافة" class="btn btn-default" />
</div>
<input class="btn btn-default" type="button" value="خروج" onclick="#("window.location.href='" + #Url.Action("LogOut", "Pharmacy") + "'");" />
</div>
</div>
}
</td>
</tr>
</table>
<div id="updatePnl">
#Html.Partial("_PartialPharmacyDetails", Model)
</div>
<br />
<br />
<br />
<div id="pnlItemsDetails">
#Html.Partial("_PartialItemsDetails", Model)
</div>
When I try to render _PharmacyDetails in my main view it won't return any records. What is the problem? and how I can fix it? Please provide me with an explanation of how this problem occurs. What did I do wrong?
Instead of using Partial method to render the partial view I used Action method:
<tr>
<td>
<div id="pDetail" style="background-color:aliceblue" method="Get">
#Html.Action("pharmacyDetials", Model)
</div>
</td>
<td>
Is there a way to store the HTML structure in a SQL view and output it with the stored HTML structure via controller?
For e.g. Here is a sample HTML which I'd like to store in a SQL View and output via controller
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<vwStudent>>" %>
<html>
<body>
<table>
<tr>
<th>
Student ID
</th>
<th>
Name
</th>
<th>
GPA
</th>
<th>
Scholarship Amount
</th>
<th>
Eligible Date
</th>
<th>
Is Senior
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.StudentID) %>
</td>
<td>
<%= Html.Encode(item.FName) %>
</td>
<td>
<%= Html.Encode(item.GPA) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:F}", item.ScholarshipAmount)) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.EligibleDate)) %>
</td>
<td>
<%= Convert.ToString(item.IsSenior) == "True" ? "Yes" : Convert.ToString(item.IsSenior) == "False" ? "No" : null%>
</td>
</tr>
<% } %>
</table>
</body>
</html>
Controller Action:
public ActionResult Students()
{
ViewData.Model = students.vwStudent.ToList();
return View();
}
65,000 records is FAR FAR too many to render on a single page... you should use pagination. There are libraries to help with this like this one: https://www.nuget.org/packages/PagedList.Mvc/
I have an existing table with many columns wherein I wanted to do a searchbox for the data inside the table.
I am using an ASPX page.
Can anyone give me an example so that I could at least refer my project there ?
Thanks.
<table class="table table-bordered">
<tr>
<th>
<%: Html.ActionLink("Employee", "Index", new { sortOrder = ViewBag.NameSortParm }) %>
</th>
<th>
<%: Html.ActionLink("Department", "Index", new { sortOrder = ViewBag.DeptSortParm }) %>
</th>
<th>
<%: Html.ActionLink("Local", "Index") %>
</th>
<th>
<%: Html.ActionLink("Position", "Index") %>
</th>
<th>
<%: Html.ActionLink("DirectLine", "Index") %>
</th>
<th>
<%: Html.ActionLink("Plant", "Index") %>
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Name) %></h6>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Dept) %> </h6>
</td>
<td>
<h5> <b><%: Html.DisplayFor(modelItem => item.Emp_Local) %></b></h5>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Position) %> </h6>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_DirectLine) %> </h6>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Entity) %> </h6>
</td>
</tr>
<% } %>
</table>
and my controller
public ActionResult Index(string nameString, string local, string dept )
{
ViewBag.Title = "Phone Directory";
List<PD_Employee> model = db.PD_Employee.ToList();
} ....... This is what I'm gonna do.
I'll gonna give bounty for this after 2 days !
If you need to get data on basis of either nameString,local or dept than you have to use or condition in linq.
List<PD_Employee> model = db.PD_Employee.Where(x => (x.Name == nameString || x.Local == local || x.Dept = dept)).ToList();
I have code below two table first is display Input field and second is display list, problem is that in input field showing the collection which is bind with that.
Code for View
<h2>AddmissionCellWithModel</h2>
<div>
<% using (Html.BeginForm("InsertN", "AddmissionCell", FormMethod.Post))
{ %>
<table>
<tr>
<td>
Student's Name:
</td>
<td>
<%= Html.TextBox("StudentName", Model)%>
</td>
<td>
Addmission Year:
</td>
<td>
<%= Html.TextBox("AddmissionYear", Model)%>
</td>
</tr>
<tr>
<td>
Father's Name:
</td>
<td>
<%= Html.TextBox("FatherName", Model)%>
</td>
<td>
Mother's Name:
</td>
<td>
<%= Html.TextBox("MotherName", Model)%>
</td>
</tr>
<tr>
<td>
Contact:
</td>
<td>
<%= Html.TextBox("Contact", Model)%>
</td>
<td>
Address:
</td>
<td>
<%= Html.TextBox("Address", Model)%>
</td>
</tr>
<tr>
<td>
SchoolName:
</td>
<td>
<%= Html.TextBox("SchoolName", Model)%>
</td>
<td>
EnrollmentId:
</td>
<td>
<%= Html.TextBox("EnrollmentID", Model)%>
</td>
</tr>
<tr>
<td colspan="4" align="right">
<input type="submit" value="Save" />
</td>
</tr>
<tr>
<td colspan="4">
</td>
</tr>
<tr>
<td colspan="4">
</td>
</tr>
</table>
<%} %>
</div>
<table>
<tr>
<th>
EnrollmentID
</th>
<th>
StudentName
</th>
<th>
SchoolName
</th>
<th>
StudentID
</th>
<th>
FatherName
</th>
<th>
MotherName
</th>
<th>
Address
</th>
<th>
Contact
</th>
<th>
ClassId
</th>
<th>
AddmissionYear
</th>
<th>
EnrollDateTime
</th>
<th>
IsActive
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: item.EnrollmentID %>
</td>
<td>
<%: item.StudentName %>
</td>
<td>
<%: item.SchoolName %>
</td>
<td>
<%: item.StudentID %>
</td>
<td>
<%: item.FatherName %>
</td>
<td>
<%: item.MotherName %>
</td>
<td>
<%: item.Address %>
</td>
<td>
<%: item.Contact %>
</td>
<td>
<%: item.ClassId %>
</td>
<td>
<%: item.AddmissionYear %>
</td>
<td>
<%: String.Format("{0:g}", item.EnrollDateTime) %>
</td>
<td>
<%: item.IsActive %>
</td>
</tr>
<% } %>
</table>
and code for Controller...
public ActionResult AddmissionCellWithModel()
{
SchoolManagementV1Entities db = new SchoolManagementV1Entities();
List<EnrollmentModel> result = (from q in db.VW_EnrollmentModel
select new EnrollmentModel()
{
EnrollmentID = q.EnrollmentId,
StudentName = q.StudentName,
StudentID = q.StudentID,
FatherName = q.FatherName,
MotherName = q.MotherName,
Address = q.Address,
Contact = q.Contact ?? 0,
AddmissionYear = q.EnrollYear,
EnrollDateTime = q.AdmissionDateTime,
IsActive = q.EnrollIsActive
}).ToList();
return View(result);
}
this return view is return the result object to the view
so some on tell me how can prevent to display the System.Collection.Generic....
Do you need a single or multiple forms?
I'm asking this because in the image I see only 1 form but you are passing a list of EnrollmentModel.
If you need only 1 form and 1 table (I think this is your case), pass to View only EnrollmentModel, not a list of them.
So change this:
List<EnrollmentModel> result = (from q in db.VW_EnrollmentModel
select new EnrollmentModel()
{
EnrollmentID = q.EnrollmentId,
StudentName = q.StudentName,
StudentID = q.StudentID,
FatherName = q.FatherName,
MotherName = q.MotherName,
Address = q.Address,
Contact = q.Contact ?? 0,
AddmissionYear = q.EnrollYear,
EnrollDateTime = q.AdmissionDateTime,
IsActive = q.EnrollIsActive
}).ToList();
return View(result);
with something like this:
var myId=7; //Example
return View(db.VW_EnrollmentModel.Single(m=>m.EnrollmentId==myId));
Note that here I'm guessing your want to display a single VW_EnrollmentModel, retrieved using EnrollmentId.
You then have to change your view setting correct model type and properties: in fact now the model is VW_EnrollmentModel so you don't have AddmissionYear but EnrollYear for example.
If this is not the expected result add a comment and I'll edit my answer.
Hope this helps,
Alberto
Greeting Everyone,
I'm having a little trouble with a partial view I'm using as an edit form. Aside from the hidden elements in the form, the model is null when passed to my EditContact post controller.
I should add, I realize there's a more seamless way to do what I'm doing using AJAX, but I think if I can solve the clunky way first it will help me understand the framcework a little better.
Here's my EditContact partial view:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WebUI.DataAccess.CONTACT>" %>
<div id="contactPartial-<%= ViewData.Model.id %>">
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("EditContact", "Investigator"))
{ %>
<%= Html.HiddenFor(Model => Model.id) %>
<%= Html.HiddenFor(Model => Model.investId) %>
<table id="EditContact-<%= ViewData.Model.id %>" width="100%">
<tr>
<th colspan="4">
<b>New Contact</b>
</th>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.type)%></b><br />
<%= Html.EditorFor(Model => Model.type, null, "contactType-" + ViewData.Model.id)%><br />
<%= Html.ValidationMessageFor(Model => Model.type) %>
</td>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.salutation)%></b><br />
<%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.firstName)%></b><br />
<%= Html.EditorFor(Model => Model.firstName, null, "contactFirstName-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.middleName)%></b><br />
<%= Html.EditorFor(Model => Model.middleName, null, "contactMiddleName-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.lastName)%></b><br />
<%= Html.EditorFor(Model => Model.lastName, null, "contactLastName-"+ViewData.Model.id)%><br />
<%= Html.ValidationMessageFor(Model => Model.lastName)%>
</td>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.title)%></b><br />
<%= Html.EditorFor(Model => Model.title, null, "contactTitle-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.phone)%></b><br />
<%= Html.EditorFor(Model => Model.phone, null, "contactPhone="+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.altPhone)%></b><br />
<%= Html.EditorFor(Model => Model.altPhone, null, "contactAltPhone-" + ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.fax)%></b><br />
<%= Html.EditorFor(Model => Model.fax, null, "contactFax-" + ViewData.Model.id)%>
</td>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.email)%></b><br />
<%= Html.EditorFor(Model => Model.email, null, "contactEmail-" + ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.comment)%></b><br />
<%= Html.EditorFor(Model => Model.comment, null, "contactComment-" + ViewData.Model.id)%>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" />
</td>
<td>
<button id="<%= ViewData.Model.id %>" class="contactEditHide">
Cancel</button>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<% } %>
</div>
When the Edit contact post controller is passed the model only id and investId have values; everything else is null. Here's how I'm rendering the partial view in my Details view:
<table width="100%">
<tr>
<th colspan="7">
<b>Contacts</b>
</th>
<th id="contactButton" class="button">
<button id="showEditContact-0" type="button" class="contactEditShow">New Contact</button>
</th>
</tr>
<tr>
<th>
Type
</th>
<th>
Name
</th>
<th colspan="2">
Title
</th>
<th>
Prim. & Alt. Phone
</th>
<th>
Fax
</th>
<th colspan="2">
Comment
</th>
</tr>
<% for (int i = 0; i < Model.CONTACTs.Count; i++)
{ %>
<tr id="contactRow-<%= Model.CONTACTs[i].id %>" class="contactRow">
<td>
<%= Html.Encode(Model.CONTACTs[i].type)%>
</td>
<td>
<%= Html.Encode(Model.CONTACTs[i].salutation)%>
<%= Html.Encode(Model.CONTACTs[i].firstName)%>
<%= Html.Encode(Model.CONTACTs[i].middleName)%>
<%= Html.Encode(Model.CONTACTs[i].lastName)%>
<br />
<a href="mailto:<%= Html.AttributeEncode(Model.CONTACTs[i].email) %>">
<%= Html.Encode(Model.CONTACTs[i].email)%></a>
</td>
<td colspan="2">
<%= Html.Encode(Model.CONTACTs[i].title)%>
</td>
<td>
Prim:<%= Html.Encode(Model.CONTACTs[i].phone)%><br />
Alt:<%= Html.Encode(Model.CONTACTs[i].altPhone)%>
</td>
<td>
<%= Html.Encode(Model.CONTACTs[i].fax)%>
</td>
<td>
<%= Html.Encode(Model.CONTACTs[i].comment)%>
</td>
<td>
<button id="<%= Model.CONTACTs[i].id %>" type="button" class="contactEditShow">Edit Contact</button>
</td>
</tr>
<tr>
<td colspan="8">
<% Html.RenderPartial("EditContact", Model.CONTACTs[i]); %>
</td>
</tr>
<% } %>
</table>
My details view is strongly tyed to an INVESTIGATOR model:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.DataAccess.INVESTIGATOR>" %>
As you can see above, my partial view is strongly typed to a CONTACTS model.
I ran into a similar problem with Model binding when, in a view, I was trying to allow for the editing all layers of an address at one time and fixed it by indexing the model, similar to the Details view above. It seems like I need to do the same thing in my partial view to ensure Model binding, but I'm not sure how to achieve this (having syntax issues).
Of course, I could be off here an need a new solution in general!
Let me know if you need more detail! Thanks!
Why are you using
<%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>
If you use it simply like this
<%= Html.EditorFor(Model => Model.salutation) %>
It should be working.
In order to map your model, your control need to have the same id as there name, and the third parameter you're using changes your control id.
Use Html.EditorFor instead of Html.RenderPartial.
See Model binding with nested child models and PartialViews in ASP.NET MVC.