output sql with html structure - asp.net-mvc

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/

Related

MVC ASPX Search box with multiple searching column

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();

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.

submit View in ASP.NET MVC

How submit list View in ASP.NET MVC. List View dont have
input type="submit" value="Save"
and I dont know where to put it.
Problematic code is:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcZezanje.Models.student1>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Studenti
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Studenti</h2>
<table class="data-table">
<tr>
<th>
Br. Indexa
</th>
<th>
Prezime
</th>
<th>
Ime
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.id_stud) %>
</td>
<td>
<%= Html.Encode(item.prezime) %>
</td>
<td>
<%= Html.Encode(item.ime) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
</asp:Content>
What do you want to submit? This code displays list of students. There is no user input. If there is no user input, there is no need to place submit button. if you want to edit user data, you'll have to do something like:
<form method="post" action="/Students/Save">
<table class="data-table">
<tr>
<th>
Br. Indexa
</th>
<th>
Prezime
</th>
<th>
Ime
</th>
</tr>
<% int i = 0; foreach (var item in Model) { %>
<tr>
<td>
<%= Html.TextBox("students[" + i + "].id_stud", item.id_stud) %>
</td>
<td>
<%= Html.TextBox("students[" + i + "].prezime", item.prezime) %>
</td>
<td>
<%= Html.TextBox("students[" + i + "].ime", item.ime) %>
</td>
</tr>
<% i++;} %>
</table>
<input type="submit" />
</form>
Here you can read about binding to list:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
It has changed a little, you don't have to specify index anymore.

Can a partial view be used to do Ajax item updates?

I have a table built from a list of defect codes.
Can part of each row load a sub-table item complete with submit buttons?
Sample table:
<table><tr>
<th>Code</th><th>Description</th>
<th>Impact to your customers</th>
<th>Impact to your associates</th>
<th>Save</th>
<th>Save Errors</th></tr>
Where the first 2 columns are populated from the lookup table, and the next 3 columns are a form so the user can set the values or update them from previous values.
Can I somehow have 3 TD items per row an individual Ajax form with the codeId embedded as a hidden value? What would my strongly typed view(s) inherit? the outer layer would inherit IEnumerable<DefectDTO>, and the partial views would inherit the AssessmentDTO type?
Here's the actual table I'm trying to get to function:
<table>
<tr>
<th>
Code
</th>
<th>
Description
</th>
<th>
Document
</th>
<th>
Customer Contact Required for Resolution
</th>
<th>
AssociateSeverity
</th>
<th>
ShareholderSeverity
</th>
<th>
CustomerSeverity
</th>
<th>
RegulatorySeverity
</th>
<th>
RootCause
</th>
<th>
Investor Requirements*
</th>
</tr>
<% foreach (var item in Model.DefectCodes) { %>
<tr>
<% using (Ajax.BeginForm("Create", new AjaxOptions() ))
{%>
<td>
<%= Html.Encode(item.Code)%>
</td>
<td>
<%= Html.Encode(item.Description)%>
</td>
<% Html.RenderPartial("Create",null, ViewData); %><!-- This is where the form is -->
<% } %>
</tr>
<% } %>
</table>
<% foreach (var item in Model.DefectCodes) { %>
<tr>
<% using (Ajax.BeginForm("Create", new AjaxOptions() ))
{%>
<!--should be easy like this!--><!--ur codeid here-->
<%=Html.HiddenFor(modelItem => item.codeid)%>
<td>
<%= Html.Encode(item.Code)%>
</td>
<td>
<%= Html.Encode(item.Description)%>
</td>
<% Html.RenderPartial("Create",null, ViewData); %><!-- This is where the form is -->
<% } %>
</tr>
<% } %>
</table>
its just solves ur hidden value! problem..
u dont need to put it into a TD cause u want it to be hidden so it will still get the value for u but will not be shown in form or in table or in view.. that way u can keep 3 TD and get more value than the shown on form.

Resources