Create paging in HTML table - html

I have Html code that generates a table that contains a lot of records.
Here is the code:
#model IEnumerable<UserContact>
#{
ViewBag.Title = "Contacts:";
}
<h2>
Contacts</h2>
<table>
<tr>
<th>
#Html.DisplayText("First Name")
</th>
<th>
#Html.DisplayText("First Name")
</th>
<th>
#Html.DisplayText("Email")
</th>
<th>
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.CheckBox("misha", new { onclick = "this.form.submit();" })
</td>
<td>
#Html.DisplayFor(modelItem => item.firstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.lastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.email)
</td>
</tr>
}
</table>
This code is contained in razor partial view page.
My question is how can I create paging in table that described above?

As per your comment i m showing complete example of datatable here :
Jquery and CSS References :-
Links :-
http://code.jquery.com/jquery-1.11.1.min.js
http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js
http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css
Your Partial View :-
#model IEnumerable<UserContact>
#{
ViewBag.Title = "Contacts:";
}
<h2>
Contacts</h2>
<table id="mydatatable">
<thead>
<tr>
<th>
#Html.DisplayText("First Name")
</th>
<th>
#Html.DisplayText("First Name")
</th>
<th>
#Html.DisplayText("Email")
</th>
<th>
</th>
</tr>
<thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.CheckBox("misha", new { onclick = "this.form.submit();" })
</td>
<td>
#Html.DisplayFor(modelItem => item.firstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.lastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.email)
</td>
</tr>
}
<tbody>
</table>
<script type="text/javascript">
$(document).ready(function(){
$('#mydatatable').dataTable();
});
</script>
Last but not the least don't forget to add references of required jquery files on your page.

//cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css
//cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js
/////////////////////////////////////
<table id="myTable">
<tr>
<th>
#Html.DisplayText("First Name")
</th>
<th>
#Html.DisplayText("First Name")
</th>
<th>
#Html.DisplayText("Email")
</th>
<th>
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.CheckBox("misha", new { onclick = "this.form.submit();" })
</td>
<td>
#Html.DisplayFor(modelItem => item.firstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.lastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.email)
</td>
</tr>
}
</table>
<script type="text/javascript">
$(document).ready(function(){
$('#myTable').dataTable();
});
</script>

Related

Pass html datalist value to Html.Action

I'm creating an HTML form that lists line items from one organization that need to be mapped to another on. My idea was to take the id of the selected option, and pass that to the action via Html.Action. This is the code I have so far;
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.newID)
</th>
<th>
#Html.DisplayNameFor(model => model.oldserviceid)
</th>
<th>
#Html.DisplayNameFor(model => model.description)
</th>
<th>
#Html.DisplayNameFor(model => model.Avg)
</th>
<th>
#Html.DisplayNameFor(model => model.Count)
</th>
<th>
#Html.DisplayNameFor(model => model.acquisitionid)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#* This is where I'm stuck *#
<form action=#Html.Action("Update", new { linemappingID = #item.LineMapID, newID = /*what goes here?*/ }) method="post" onsubmit="return updateForm()" id="#item.LineMapID">
<input list="serviceList" name="serviceList">
<datalist id="serviceList">
#foreach (var serv in ViewBag.serviceList)
{
#* Pass the ID value from here to the Html.Action *#
<option value="#serv.serviceid - #serv.servicedesc" id="#serv.serviceid">#serv.serviceid - #serv.servicedesc</option>
}
</datalist>
<input type="submit" value="update" />
</form>
</td>
<td>
#Html.DisplayFor(modelItem => item.oldserviceid)
</td>
<td>
#Html.DisplayFor(modelItem => item.description)
</td>
<td>
$#Html.DisplayFor(modelItem => item.Avg)
</td>
<td>
#Html.DisplayFor(modelItem => item.Count)
</td>
<td>
#Html.DisplayFor(modelItem => item.acquisitionid)
</td>
</tr>
}
</table>

bind modal to row click in datatable

Helo,
i have a datatable that contains data from my database. in each row there is a details button. i want on button click to open a modal with the details of the clicked rows.
i tried to create instance of modals for each row on page load but it is slowing down the website.
is there a way to populate a modal according to a row ID?
this is my table
<table id="tblMessagesDetail" class="table table table-hover table-striped table-condensed export-table" style="width: 100%;">
<thead>
<tr>
<th>
#*#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Id)*#
#ViewRes.SharedStrings.SmsID
</th>
<th>
#*#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Account.Project.Customer.Name)*#
#ViewRes.SharedStrings.CustomerName
</th>
<th>
#*#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Account.Project.Name)*#
#ViewRes.SharedStrings.ProjectName
</th>
<th>
#*#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Account.Login)*#
#ViewRes.SharedStrings.Account
</th>
<th>
#Html.DisplayNameFor(model => model.SmsList[0].MSISDN)
</th>
<th>
#*#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.SmsStatus.Value)*#
#ViewRes.SharedStrings.smsStatus
</th>
<th>
#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.EventReason)
</th>
<th>
#*#Html.DisplayNameFor(model => model.SmsList[0].SendDate)*#
#ViewRes.SharedStrings.SendDate
</th>
<th>
#*#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.SentDate)*#
#ViewRes.SharedStrings.SentDate
</th>
<th>
#Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Ticket)
</th>
<th>
#Html.DisplayNameFor(model => model.SmsList[0].ExtId)
</th>
<th>
#Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.NotificationLevel)
</th>
<th>
#Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.OADC)
</th>
<th>
#Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.Message)
</th>
<th>
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.SmsList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Customer.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Account.NMGLogin)
</td>
<td>
#Html.DisplayFor(modelItem => item.MSISDN)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.SmsStatus.Value)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.EventReason)
</td>
<td>
#Html.DisplayFor(modelItem => item.SendDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.SentDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Ticket)
</td>
<td>
#Html.DisplayFor(modelItem => item.ExtId)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMTContent.NotificationLevel)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMTContent.OADC)
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMTContent.Message)
</td>
<!-- Button trigger modal -->
<td>
#*#using (Html.BeginForm("FillDetailModal", "Statistics", FormMethod.Post))
{*#
<button id="btnDetails" type="submit" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal-#item.SmsMTId">
<i class="glyphicon glyphicon-th-list"></i> #ViewRes.SharedStrings.btnDetails
</button>
</td>
</tr>
}
</tbody>
and this is my modal
<div class="modal fade" draggable="true" id="myModal-#item.SmsMTId" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div id="modalDialog" draggable="true" class="modal-dialog" style="padding-bottom:0px">
<div class="modal-content">
<div class="modal-header" style="background-color: #428bca; color: white; ">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title" id="myModalLabel">
Message Detail ID: '#item.SmsMTId' - Ticket ##item.SmsMT.Ticket - Project #item.SmsMT.Account.Project.Name
</h4>
</div>
<div class="modal-body" style="padding: 0px;">
<table style="width:100%;" class="table table table-hover table-striped table-condensed export-table" border="0">
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.SmsID</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Id)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.CustomerName</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Customer.Name)
</td>
</tr>
<tr>
<td align="left">
<label> #ViewRes.SharedStrings.ProjectName</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Name)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.Account</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Account.NMGLogin)
</td>
</tr>
<tr>
<td align="left">
<label> #Html.DisplayNameFor(model => model.SmsList[0].MSISDN)</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.MSISDN)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.smsStatus</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.SmsStatus.Value)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.EventReason</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.EventReason)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.SendDate</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SendDate)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.SentDate</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.SentDate)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.TicketId</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Ticket)
</td>
</tr>
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.ExternalID</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.ExtId)
</td>
</tr>
<tr>
<td align="left">
<label style="width:160px;">#ViewRes.SharedStrings.NotificationLevel</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMTContent.NotificationLevel)
</td>
</tr>
<tr>
<td align="left">
<label> #Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.OADC)</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMTContent.OADC)
</td>
</tr>
<tr>
<td align="left">
<label>#Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.Message)</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMTContent.Message)
</td>
</tr>
<tr>
<td align="left">
<label> #ViewRes.SharedStrings.binaryMessage</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMTContent.Binary)
</td>
</tr>
</table>
</div>
<div class="modal-footer" style="margin: 0px; padding: 9px 30px 10px;">
<button type="button" class="btn btn-default" data-dismiss="modal">
#ViewRes.SharedStrings.btnClose
</button>
</div>
</div>
</div>
</div>
how can i pass the value of the row and then do a link wuery on the database to extract the appropriate data?
I appreciate any help i can get
You can actually bind your id to data-whatever attribute and put it in a table row or even in a column.
<tr data-toggle="modal" data-target="#myModalIdHere" data-whatever="myDataIdHere">
in jQuery:
$('#myModalIdHere').on('show.bs.modal', function (event) {
// this is the trigger
var tableRow = $(event.relatedTarget);
// get myDataIdHere to data-whatever attribute
var myDataIdHere = tableRow.data('whatever');
// updating modal's content
// bind myDataIdHere to myInputIdHere
var modal = $(this);
modal.find('.modal-body #myInputIdHere').val(myDataIdHere);
});
Query all data and put icons in the last column using while loop. With this u can create a table with all your data and the icons will serve us the buttons for your view, delete or edit
while($row = $sql->fetch()){
$id = $row['id'];
$company = $row['company_name'];
$domain = $row['domain_name'];
$industry = $row['industry_type'];
$websitet = $row['website_type'];
$websitep = $row['website_purpose'];
$audience = $row['target_audience'];
$web_pages = $row['web_pages'];
$color = $row['color_scheme'];
$style = $row['style'];
$addons = $row['addons'];
$url = $row['url_of_existing'];
$like_dislike = $row['like_dislike_existing_website'];
$sample = $row['sample_website'];
echo '<tr><td>'.$id.'</td>';
echo '<td>'.$company.'</td>';
echo '<td>'.$domain.'</td>';
echo '<td>'.$industry.'</td>';
echo '<td><img src=img/view.png width=20px height=20px id="view"></td>';
echo '<td><img src="img/delete.png" width=20px height=20px name="delete"></td></tr>';
?>
the <a href...?id='.$id.'" part is to send the id using the get method.. look it up if you dont know about it.. pretty much this will solve your problem.

Checkbox Deactiving Rows by ID (View/Controller) in MVC 5

I have a table in my view with a column of Checkboxes. I want the user to be able to select as many Checkboxes as they'd like and click a button at the bottom to deactivate them (Users in this model can be suspended/deactivated and reinstated/activated). I don't have any errors, but nothing is happening when I click on the buttons. I attached my code below. Thanks.
View
#using (Html.BeginForm("CheckBoxDeactivate"))
{
<table class="table">
<tr>
<th>
Select Users
</th>
<th>
#Html.DisplayNameFor(model => model.ID)
</th>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.LastName)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th>
#Html.DisplayNameFor(model => model.UserName)
</th>
<th>
#Html.DisplayNameFor(model => model.suspended)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
<input type= "checkbox" name="manageUsers" value="#item.ID" />
</td>
<td>
#Html.DisplayFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email)
</td>
<td>
#Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
#Html.DisplayFor(modelItem => item.suspended)
</td>
</tr>
}
</table>
<br />
<input type="button" value="Add Roles" onclick="location.href'#Url.Action("CheckBoxDeactivate", "User")'" />
Controller
[HttpPost]
public ActionResult CheckBoxDeactivate(int[] ids)
{
tbl_Reg_User OAWUser = new tbl_Reg_User();
foreach (var id in ids)
{
OAWUser = db.tbl_Reg_User.Single(o => o.ID == id);
OAWUser.suspended = true;
db.SaveChanges();
}
ViewBag.Message = "Users deactivated successfully!";
return View();
}
<input type= "checkbox" name="manageUsers" value="#item.ID" />
This line should be
<input type= "checkbox" name="ids" id="ids" value="#item.ID" />
I think you are not getting the selected checkboxes in controller.
View
#using (Html.BeginForm("CheckBoxDeactivate","Home"))
{
<table class="table" border="1" style="border:1px solid black;border-collapse:collapse;">
<tr>
<th>Select Users
</th>
<th>
Sr.No.
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Phone
</th>
<th>
IsActive
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input type= "checkbox" name="ids" id="ids" value="#item.Sr_No" />
</td>
<td>
#item.Sr_No
</td>
<td>
#item.FirstName
</td>
<td>
#item.LastName
</td>
<td>
#item.Phone
</td>
<td>
#item.IsActive
</td>
</tr>
}
</table>
<br />
<input type="submit" value="Add Roles" onclick="#Url.Action("CheckBoxDeactivate", "User")" />
}
Controller
[HttpPost]
public ActionResult CheckBoxDeactivate(int[] ids)
{
Temp OAWUser = new Temp();
if (ids != null)
{
foreach (var id in ids)
{
//Your code goes here
}
ViewBag.Message = "Users deactivated successfully!";
}
return RedirectToAction("Temp");
}
This piece of code did the job for me

One submit button for Form and textbox MVC4

I am using MVC4.
I have view of form using #using (Html.BeginForm)
But I want to use table instead of fieldset
The view look great but then I had to have the submit button (in every record)as shown in my code .
I want only one button to submit ? So I try to put the submit out
of (Html.BeginForm) but then it will not work!!?
Also I have textbox for price ...And I want to be linked to the same submit button
this is my view :
<p>Enter the price please :</p>#Html.TextBox("price")
<fieldset>
<legend></legend>
<table>
<tr>
<th>
Title1
</th>
<th>
Title1
</th>
<th>
Title3
</th>
<th>
button
</th>
</tr>
#using (Html.BeginForm("Action", "Contrroler"))
{
int index = 0;
foreach (var req in Model.SelectedHome.Descreption)
{
<tr>
<td>
#Html.DisplayFor(m => m.homenmae)
</td>
<td>
#Html.DisplayFor(m => m.place)
</td>
<td>
#Html.EditorFor(m => m.rommsnumber)
</td>
<td>
<button type="submit" class="button "> click </button>
<td>
</tr>
index++;
}
}
</table>
</fieldset>
You can't submit a form with submit button out of form without some additional code
#using (Html.BeginForm("Action", "Contrroler", FormMethod.Post, new {id='myForm'} ))
{
<p>Enter the price please :</p>#Html.TextBox("price")
<fieldset>
<legend></legend>
<table>
<tr>
<th>
Title1
</th>
<th>
Title1
</th>
<th>
Title3
</th>
<th>
button
</th>
</tr>
int index = 0;
foreach (var req in Model.SelectedHome.Descreption)
{
<tr>
<td>
#Html.DisplayFor(m => m.homenmae)
</td>
<td>
#Html.DisplayFor(m => m.place)
</td>
<td>
#Html.EditorFor(m => m.rommsnumber)
</td>
</tr>
index++;
}
}
</table>
<button id='submitForm'>Submit</button>
JS:
$('#submitForm').click(function(){
$('#myForm').submit();
});
Put the submit button outside the for loop but within the form

Server Error in '/' Application MVC 4

I have created a search function on my website that uses a partial view for users to input their search criteria, the results of the search should be displayed on the results page. My issues is that when the search is run the results page doesn't display instead I get a Server Error in '/' Application. error.
Any help would be great full
_SearchView
#using (Html.BeginForm("Results", "Search", FormMethod.Get))
{
<div class="search-button1">
<input type="text" class="form-control" placeholder="Search" name="SearchString" id="SearchString">
</div>
}
Search Controller
namespace BiteWebsite.Controllers
{
public class SearchController : Controller
{
//
// GET: /Search/
public ActionResult _SearchView()
{
return View();
}
[HttpPost]
public ActionResult Results(string SearchString)
{
using (var objCtx = new ApplicationDbContext())
{
var Restaurant = from u in objCtx.Restaurants
select u;
if (!String.IsNullOrEmpty(SearchString))
{
Restaurant = Restaurant.Where(s => s.Name.Contains(SearchString));
}
return View(Restaurant);
}
}
}
}
Results page
#model IEnumerable<BiteWebsite.Models.Restaurant>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Cuisine)
</th>
<th>
#Html.DisplayNameFor(model => model.StreetNumber)
</th>
<th>
#Html.DisplayNameFor(model => model.StreetName)
</th>
<th>
#Html.DisplayNameFor(model => model.County)
</th>
<th>
#Html.DisplayNameFor(model => model.PostCode)
</th>
<th>
#Html.DisplayNameFor(model => model.Contactnumber)
</th>
<th>
#Html.DisplayNameFor(model => model.Website)
</th>
<th>
#Html.DisplayNameFor(model => model.UserID)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Cuisine)
</td>
<td>
#Html.DisplayFor(modelItem => item.StreetNumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.StreetName)
</td>
<td>
#Html.DisplayFor(modelItem => item.County)
</td>
<td>
#Html.DisplayFor(modelItem => item.PostCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.Contactnumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.Website)
</td>
<td>
#Html.DisplayFor(modelItem => item.UserID)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.id }) |
#Html.ActionLink("Details", "Details", new { id=item.id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>