MVC2 code conversion in Razor - asp.net-mvc-2

I am using someones code for paging. His code is in MVC 2 and I want it in MVC3 Razor. Problem is with syntax.
Below is the code in mvc need someone to fix syntax for razor please.
Problem is in this line
IList<Customer> customers = (IList<Customer>)Model.Data;
//Can't use Model.Data directly. Doesn't pick up generic type.
IList<Customer> customers = (IList<Customer>)Model.Data;
foreach (Customer item in customers) { %>
<tr onclick="onRowClick(<%= item.ID %>)">
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.ID}) %> |
<%= Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
</td>
<td>
<%= Html.Encode(item.ID) %>
</td>
<td>
<%= Html.Encode(item.FirstName) %>
</td>
</tr>
<% } %>

The line could be translated like this:
# {
IList<Customer> customers = (IList<Customer>)Model.Data;
}
and then:
#foreach (Customer item in customers) {
<tr onclick="onRowClick(#item.ID)">
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.ID })
#:|
#Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
<td>
#item.ID
</td>
<td>
#item.FirstName
</td>
</tr>
}
I would also benefit from this migration to improve this code. Currently you are using loops in your views which are ugly and could be replaced with display templates.
So, in your main view:
#Html.DisplayFor(x => x.Data)
and in ~/Views/Home/DisplayTemplates/Customer.cshtml:
#model YourApp.Models.Customer
<tr onclick="onRowClick(#Model.ID)">
<td>
#Html.ActionLink("Edit", "Edit", new { id = Model.ID })
#:|
#Html.ActionLink("Delete", "Delete", new { id = Model.ID })
</td>
<td>
#Model.ID
</td>
<td>
#Model.FirstName
</td>
</tr>

Related

Can not edit the form from sails.js

Hey guys I'm really new at sails.js and I'm using version 1.0 of it. I just tried to create a CRUD application on adding article to my list and showing them. All goes right but when I want to delete or edit it says 404 error. I am going through this tutorial series and as I know this series is using sails js version 0.12.
I am using mongodb as my database as a default. Data insertion is going well but problem is when I wanted to edit or delete it. Here I think the problem is getting the id value from the url. I skipped the edit portion here and posted the delete portion here.
api/controllers/ ArticleController.js
module.exports = {
delete: function(req, res){
Articles.destroy({id:req.params.id}).exec(function(err){
if(err){
res.send(500,'Database Error');
}
res.redirect('/articles/list');
});
return false;
}
};
api/models/ Articles.js
module.exports = {
attributes: {
title:{
type:'string'
},
body:{
type:'string'
}
},
};
views/pages list.ejs
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="POST">
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
</tr>
<% }) %>
</tbody>
</table>
config/route.js
module.exports.routes = {
'/': {
view: 'pages/homepage'
},
};

Footable table timeout when row count is over 1000

I have tried to implement footable pluglin on an MVC application and it currently has 3000+ users which fail to load and the browser page times out. For the User Admin section I have created the following View:
#model IEnumerable<MyBudgetWeb.Models.ApplicationUser>
#{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
$('.footable').footable();
});
</script>
<br /><br />
<h2>Bill Pay Users</h2>
<input id="filter" type="text" placeholder="Filter" />
<br /><br />
#if (Model.Count() > 0)
{
<table class="table" data-filter="#filter" data-page-size="50">
<thead>
<tr>
<th data-type="numeric">Customer Number</th>
<th data-type="numeric">Account Name</th>
<th data-type="numeric" data-hide="phone">Go Live Date</th>
<th data-type="numeric" data-hide="phone">Online Live Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#item.CustomerNumber</td>
<td>#item.AccountName</td>
<td>#item.GoLiveDate</td>
<td>#item.OnlineCreationTimestamp</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>
}
</tbody>
<tfoot class="hide-if-no-paging">
<tr>
<td colspan="12" class="text-center">
<ul class="pagination"></ul>
</td>
</tr>
</tfoot>
</table>
}
else
{
<h2>You are still awaiting users to be added. All users will be displayed here.</h2>
}
My controller looks as follows:
public async Task<ActionResult> Index()
{
return View(await UserManager.Users.ToListAsync());
}
Is there any way workarounds to prevent this?
there are 2 option to solve:
1. PHP Side script, by setting the time limit of execution. Use this code on top your PHP script:
set_time_limit(0);
Use Footable AJAX what describe here:
http://fooplugins.github.io/FooTable/

MVC Html.CheckBox list and Jquery Post

my code is
<% using (Html.BeginForm())
{%>
<table>
<tr>
<th>
LabelID_FK
</th>
<th>
LabelName
</th>
<th>
LabelIsDocument
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%: item.LabelID_FK %>
</td>
<td>
<%: item.LabelName %>
</td>
<td>
<%-- <input type="checkbox" value="df" id="chk" onclick="check()" />--%>
<%=Html.CheckBox("chk_"+ item.LabelID_FK)%>
</td>
</tr>
<% } %>
</table>
<p>
<input type="button" value="submit" id="btn" />
</p>
which show checkbox list for document label which user can select it .
i want pass data list which user select checkbox it use jquery post
what i do?
i use this code when user click on button and work very good
[HttpPost]
public ActionResult DocumentLabel(FormCollection model)
{
for (int i = 0; i < model.Count; i++)
{
AriaCRMEntities aria = new AriaCRMEntities();
DocumentLabel label = new DocumentLabel();
string lbl = model[i].ToString();
string[] check = lbl.Split(',');
bool chk = Convert.ToBoolean(check[0]);
string name = model.Keys[i].ToString();
string[] n = name.Split('_');
string lblid = n[1];
if (chk)
{
label.LabelID_FK = Int32.Parse(lblid);
Guid id = Guid.NewGuid();
label.DocumentID_FK = id;
aria.DocumentLabels.AddObject(label);
aria.SaveChanges();
}
}
return Content("0ok");
}
but i want jquery post i need array check box whit select it to pass it to controller?

Foreach(Var item in model) problem

<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "EditUser", "Profile", new { id = item.UserID },null)%>
</td>
<td>
<%: Html.ActionLink("NewUser", "Create", "Profile", new { id = item.Customer },null)%>
</td>
<td>
<%: item.UserName %>
</td>
I am getting the NewUser Tab with every Edit Tab n also Username tab..I just need it once newhere in the Page, How Can i do that..n this is been done in a
I'm not sure what you're asking but if you only want one edit link for the passed in model then you should use the Model object.
Update based on new information in question
You need to pull the new user action link out of the foreach and instead of using item you use Model instead. If your Model does not contain a Customer object you should modify your ViewModel to include it or pass it in using the ViewData.Customer = SomeCustomerValue and then instead of using the model you would use View.Customer in your aspx page like so
Use this method if you're passing in the Customer via the ViewData
<%: Html.ActionLink("NewUser", "Create", "Profile", new { id = View.Customer },null)%>
(in your controller you would do this)
public ActionResult SomeAction() {
ViewData.Customer = CurrentCustomer;
return View(ListOfUsers);
}
Use this method if your Model includes the Customer property
<%: Html.ActionLink("NewUser", "Create", "Profile", new { id = Model.Customer },null)%>
<% foreach (var item in Model.Users) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "EditUser", "Profile", new { id = item.UserID },null)%>
</td>
<td>
<%: item.UserName %>
</td>
</tr>
<% } %>

How to filter the dropdown list by using other dropdown list in asp.net MVC web-application

everyone, I encounter a problem when I try to filter the drop down list.
The situation is I wish to filter the Agent dropdown list by using the Company dropdown list in same view/form. Since I need to display the Agents that only belong to the selected Company. But I have no idea to do that.
Any solution, please?
First add Method in Controller as follows
MvcApplication2.Models.MySampleDBEntities db = new Models.MySampleDBEntities();
public ActionResult SearchNames(string ddlcontent)
{
var list = new List<string>();
var nameqry = from n in db.Images
select n.PlayerName;
list.AddRange(nameqry.Distinct());
ViewBag.ddlcontent = new SelectList(list);
var names = from m in db.Images
select m;
if (string.IsNullOrEmpty(ddlcontent))
return View(names);
else//Filter content basedon dropdownlist selected item
return View(names.Where(s => s.PlayerName.Contains(ddlcontent)));
}
then bind it to view as follows
#model IEnumerable<MvcApplication2.Models.Image>
#{
ViewBag.Title = “SearchNames”;
}
<h2>SearchNames</h2>
#using (#Html.BeginForm(“SearchNames”, “Names”, FormMethod.Get))
{
#Html.DropDownList(“ddlcontent”, “All”)<input type=”submit” value=”Filter” />;
}
<table border=”4″ style=”border: medium dashed #FF0000″>
<tr>
<th>
PlayerName
</th>
<th>
Play
</th>
<th>
CountryName
</th>
<th>
Image
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.PlayerName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Play)
</td>
<td>
#Html.DisplayFor(modelItem => item.CountryName)
</td>
<td>
<img src=”#item.ImagePath” height=”100″ width=”100″/>
</td>
</tr>
}
</table>
2 options really...either split the server-based form post into multiple steps, or use jquery to handle this behavior JIT