MVC ASPX Search box with multiple searching column - asp.net-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();

Related

output sql with html structure

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/

MVC2 Application Can we make a page where Input and display list in same place

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

Hiding column in table based on role in MVC

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.

MVC Error: model item of type 'System.Collections.Generic.IEnumerable`1 required

I'm doing some modifications to a system, and I've run into an exception that I was hoping someone could help with?? The error I'm getting is the following:
Exception Details:
System.InvalidOperationException: The
model item passed into the dictionary
is of type
'System.Collections.Generic.List1[System.String]',
but this dictionary requires a model
item of type
'System.Collections.Generic.IEnumerable1[MyApp.Data.aspnet_Users]'.
Basically what I want to do is display a list of Inactive users. Here is the controller I've written for that:
public ActionResult InactiveUsers()
{
using (ModelContainer ctn = new ModelContainer())
{
aspnet_Users users = new aspnet_Users();
DateTime duration = DateTime.Now.AddDays(-3);
var inactive = from usrs in ctn.aspnet_Users
where usrs.LastActivityDate <= duration
orderby usrs.LastActivityDate ascending
select usrs.UserName;
return View(inactive.ToList());
}
}
From here then I added a partial view. Here is the code for it if anyone is interested. Just the usual Add View --> List.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>>" %>
<table>
<tr>
<th></th>
<th>
ApplicationId
</th>
<th>
UserId
</th>
<th>
UserName
</th>
<th>
LoweredUserName
</th>
<th>
MobileAlias
</th>
<th>
IsAnonymous
</th>
<th>
LastActivityDate
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "Edit", new { id=item.UserId }) %> |
<%: Html.ActionLink("Details", "Details", new { id=item.UserId })%> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.UserId })%>
</td>
<td>
<%: item.ApplicationId %>
</td>
<td>
<%: item.UserId %>
</td>
<td>
<%: item.UserName %>
</td>
<td>
<%: item.LoweredUserName %>
</td>
<td>
<%: item.MobileAlias %>
</td>
<td>
<%: item.IsAnonymous %>
</td>
<td>
<%: String.Format("{0:g}", item.LastActivityDate) %>
</td>
</tr>
<% } %>
</table>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
And for completeness here is the Index page where I render the partial:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Administrator.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<% Html.RenderAction("InactiveUsers"); %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
If anyone has any advice I'd be very grateful :)
Hi i think its because in your partial view type is System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>>
but you select user names
select usrs.UserName;
return View(inactive.ToList());
Try change it to select usrs; but not usrs.UserName

Model binding when rendering a partial view multiple times (partial view used for editing)

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.

Resources