Dynamically Set View Title - c#

Not even sure if I asked the question the right way. Been looking at this for about an hour and its too simple to take to long. Trouble is I am too simple to know the answer or even how to correctly phrase a search to find the answer.
I have a history of jobs completed for a site set up.
Controller:
public async Task<IActionResult> JobSiteHistory(int id, int? page)
{
var jobs = from j in _context.Job
.Include(j => j.Site)
.Include(j=>j.WaterBody)
.Where(j=>j.Site.SiteID==id)
.OrderByDescending(j=>j.BookingDate)
select j;
int pageSize = 9;
return View(await PaginatedList<Job>.CreateAsync(jobs.AsNoTracking(), page ?? 1, pageSize));
}
This is returning the correct records all good.
I then have a view set up:
<h2> Site Jobs History</h2>
<p>
<a asp-action="Create">Add New Job</a>
</p>
<table class="table">
<thead>
<tr>
<th>Booking Date</th>
<th>Job Number</th>
<th>Waterbody</th>
<th>Job Description</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model) {
<tr>
<td>#Html.DisplayFor(modelItem => item.BookingDate)</td>
<td>#Html.DisplayFor(modelItem => item.JobNumber)</td>
<td>#Html.DisplayFor(modelItem => item.WaterBody.WBName)</td>
<td>#item.JobDescription.Substring(0, Math.Min(item.JobDescription.Length, 30))</td>
<td>
<a asp-action="Edit" asp-route-id="#item.JobID">Edit</a> |
<a asp-action="Details" asp-route-id="#item.JobID">Details</a> |
<a asp-action="Delete" asp-route-id="#item.JobID">Delete</a> |
</td>
</tr>
}
</tbody>
</table>
This is working wellish so far.
All I want to do is add something like:
#Html.DisplayFor(ModelItem=>item.Site.SiteName)
To the <h2> element. I know this wont work as typed, thanks for thinking that.
I just cant see a way to add it. I considered ViewData, but may be using it wrong as I cant get it to populate with SiteName.
Is there a way to do this or am I thinking all ass about as usual?

The easiest change would be to use this:
<h2>#Html.DisplayFor(m => m[0].Site.SiteName);</h2>
Other options:
Is there any reason why you can't use the ViewBag?
In controller:
ViewBag.SiteName = Site.name
In view:
<h2>#ViewBag.SiteName</h2>
If you must use your model to pass the whole site object then change your view model that you pass to the view.
You are currently returning a list of jobs with the site object for each job, but it looks like you only need it once.
I would change your view model to be something like:
public class SiteJobsHistoryModel
{
public Site Site { get; set;}
public PaginatedList<Job> Jobs { get; set; }
}
Then you don't have to include the site on your query, and just retrieve it once from the database:
var site = _context.Site.Single(j => j.Site.SiteID==id);
var jobs = from j in _context.Job
//.Include(j => j.Site) -- this can be removed
.Include(j=>j.WaterBody)
.Where(j=>j.Site.SiteID==id)
.OrderByDescending(j=>j.BookingDate)
select j;
return View(new SiteJobsHistoryModel
{
Site = site,
Jobs = await PaginatedList<Job>.CreateAsync(jobs.AsNoTracking(), page ?? 1, pageSize)
});
Then for the title in <h2> tag you can use:
#Html.DisplayFor(ModelItem=>model.Site.SiteName)
And your foreach loop becomes:
#foreach (var item in Model.Jobs)

Related

Increase Dropdown Filtering Efficiency

I need help making my list filtering more efficient.
I have a ASP.NET MVC application where there is a view for records. I've added a filter option that has a number of dropdowns where, based on the values selected in the dropdowns, filter out the result set of the records. The way I've constructed this filter was based off another filter I've seen but I know there has to be a more effective way to do what I would like to do without taking so many steps or using up as much resource. I can tell this isn't very scalable.
Right now the process involves a number of components.
The view, where the results are displayed. Reports.cs
The viewModel, to provide data to the view. ReportViewModel.cs
The view references a controller that uses a method that references a service method. ReportController.cs using GetReportFilters()
The service method refers to another method that pulls all relevant files and filters them GetReportFilters uses RetrieveFilteredReports() located in ReportsService.cs
RetrieveFilteredReports() references RetrieveReportsForFilter() Where RetrieveReportsForFilter runs a query against the DB and pulls all needed files. Here are the corresponding code snippets. (edited to save space, change some namespaces, and DB name)
This is a section of the view Reports.cs
<form class="form" asp-action="GetReportFilters" asp-controller="REPORTS">
<div class = "row">
<div class="form-group">
<p>
Types: #Html.DropDownList("TypeDropdown", new ReportApp.Services.ReportService().GetDropDown("Type"), "select", new { id = "type" })
</p>
</div>
<div class="form-group">
<p>
Shift: #Html.DropDownList("ShiftDropdown", new ReportApp.Services.ReportService().GetDropDown("Shift"), "select", new { id = "shifts" })
</p>
</div>
</div>
</form>
<table class="table table-striped" id="myTable">
<thead>
<tr>
<th>
Type
</th>
<th>
Shift
</th>
<th>
Edit
</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tr class="cost">
<td>
#Html.DisplayFor(modelItem => item.TypeId)
</td>
<td>
#Html.DisplayFor(modelItem => item.ShiftTimeFound)
</td>
<a class="anchorDetail" href="#Url.Action("GetSpecificReport", "Report", new {ReportId = item.ReportId})">
<i class="fa fa-eye" style="font-size: 30px;"></i>
</a>
</td>
</tr>
}
</tbody>
Here is the snippet from the method referenced in the controller
public ViewResult GetReportFilters(string TypeDropdown, string ShiftDropdown)
{
AppContexts.Current.Session.SetObject("TypeDropdown", TypeDropdown);
AppContexts.Current.Session.SetObject("ShiftDropdown", ShiftDropdown);
var viewModel = new ReportService().RetrieveFilteredReports(TypeDropdown, ShiftDropdown);
return View("ReportHistory", viewModel.ToPagedList(p ?? 1, s ?? 10));
}
Here is the RetrieveFilteredReports as referenced in the above method. Also here is and RetrieveReportsForFilter which is referenced in RetrieveFilteredNdrs
public List<ReportViewModel> RetrieveFilteredReports(string TypeName, string Shift)
{
var listOfReports = new ReportService().RetrieveReportsForFilter();
Dictionary<string, string> filterDictionary = new Dictionary<string, string>
{
{ "TypeName", TypeName },
{ "Shift", Shift },
};
foreach (KeyValuePair<string, string> entry in filterDictionary)
{
if (entry.Value != null)
{
switch (entry.Key)
{
case "TypeName":
listOfReports = listOfReports.Where(x => x.TypeId == entry.Value).ToList();
break;
case "Shift":
listOfNdrs = listOfNdrs.Where(x => x.ShiftTimeId == entry.Value).ToList();
break;
return listOfReports.Select(x => new ReportViewModel
{
TypeItemsId = x.TypeId ,
ShiftId = x.ShiftTimeId
}).ToList();
}
public List<ReportViewModel> RetrieveReportsForFilter()
{
var listOfAudits = new List<ReportViewModel>();
using var context = new ReportContext();//contains the formatting for fields
var dropdowns = context.DB.AsNoTracking().ToList();//replaced actually db name with just DB
var query = context.Reports.AsNoTracking().ToList();
listOfAudits = query.Select(x => new ReportViewModel
{
TypeId = x.ReportType,
ShiftTimeId = x.ShiftTimeFound,
}).ToList();
return listOfAudits;
}
As you can see the code bounces around quite a bit. As more and more entries are added to the DB it'll get continually slower until it becomes unbearable. How can I improve the efficiency of this process for scalability?
If there is any more needed information please let me know.

foreach (var in item) and exclusion

Small Question,
I am having the following in my c# mvc project code:
div class="card-body">
#if (User.IsInRole("Secretaris"))
{
<table class="table">
<tr>
<th>#Html.DisplayNameFor(model => model.FirstName)</th>
</tr>
#foreach (var item in Model)
{
if (User.IsInRole("Secretaris"))
{
<tr>
<td>#Html.DisplayFor(modelitem => item.FirstName)</td>
</tr>
}
}
</table>
}
</div>
When I run the code everything works fine but when I go into the foreach it still gives all the names and not only the names that have the role "Secretaris". Hope someone can help me in what I am doing wrong.
Thanks in advance.
Roel Knippen
Since your Model has a .Firstname property I assume your model represents a person, likely a user.
#User represents the currently logged in user - NOT the user from your model.
You will need to include role information in your model if you want to work with users/permissions in your model. Something like
if (modelItem.Roles.Contains("Sec...")) { }
To show the table to begin with, you check to see if the User has the Secretaris role. So it either will or will not show, depending on that role.
However, then within the foreach loop you check to see if the user is in that role again, which is redundant because that code won't even run if it's not due to your enclosing if statement.
I'm assuming you want to run some kind of check on the "item" object that is an element of the Model.
div class="card-body">
#if (User.IsInRole("Secretaris"))
{
<table class="table">
<tr>
<th>#Html.DisplayNameFor(model => model.FirstName)</th>
</tr>
#foreach (var item in Model)
{
if (item.role == "Secretaris")
{
<tr>
<td>#Html.DisplayFor(modelitem => item.FirstName)</td>
</tr>
}
}
</table>
}
</div>
Maybe something like this where you check if the item.role is secretaris?

Implement Sorting With ASP.NET Core Razor Pages

I have written the following Code to implement sorting. I want to maintain the sortingData(OrderDirection,SortField) between two requests for Sorting. Somehow I'm not able to achieve that.
//In .cshtml Page
#{
SortingPagingInfo info = (SortingPagingInfo)ViewData["SortingPagingInfo"];
}
<form method="post">
#Html.Hidden("SortField", info.SortField)
#Html.Hidden("SortDirection", info.SortDirection)
#Html.Hidden("PageCount", info.PageCount)
#Html.Hidden("PageSize", info.PageSize)
#Html.Hidden("CurrentPageIndex", info.CurrentPageIndex)
<table class="table">
<thead>
<tr>
<th>
<a asp-page="./Page" asp-route-sortData="#info">
#Html.DisplayNameFor(model => model.Tabl[0].Col1)
</a>
</th>
<th>
<a asp-page="./Page" asp-route-sortData="#info">
#Html.DisplayNameFor(model => model.Tabl[0].Col2)
</a>
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Table)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Col1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Col2)
</td>
</tr>
}
</tbody>
</table>
</form>
-----In cshtml.cs Page
The get Request is as below:
public async Task OnGetAsync(SortingPagingInfo sortData)
{
SortingPagingInfo info = new SortingPagingInfo();
if (string.IsNullOrEmpty(sortData.SortDirection))
{
info.SortField = "Col1";
info.SortDirection = "OrderBy";
info.PageSize = 10;
info.PageCount = Convert.ToInt32(Math.Ceiling((double)(_context.Tab.Count()
/ info.PageSize)));
info.CurrentPageIndex = 0;
this.sortPageData = info;
ViewData["SortingPagingInfo"] = info;
}
else
{
info = (SortingPagingInfo)ViewData["SortingPagingInfo"];
}
tab1= await _context.Tabl.OrderByDynamic(info.SortField, info.SortDirection).ToListAsync();
}
I'm trying to pass the object and maintain it in ViewData so that It could be accessed. But everytime, only null value is returned. Is there any better way for implementing Sorting with Razor Pages or If this could be made to work?
You cannot simply dump an object as a route data param on a link. What will actually happen here is that Razor will simply call ToString on your info instance. You have a few options:
Break out each property and pass it individually:
<a asp-page="./Page" asp-route-sortField="#info.SortField" asp-route=sortDirection="#info.SortDirection" asp-route-pageCount="#info.PageCount" asp-route-pageSize="#info.PageSize" asp-route-currentPageIndex="#info.CurrentPageIndex">
Serialize the object and then deserialize it in your action:
<a asp-page="./Page" asp-route-sortData="#JsonConvert.SerializeObject(info)">
Then, your action would need to be changed to accept a string:
public async Task OnGetAsync(string sortData)
And inside the action, you'd then deserialize that string back into an instance of SortingPagingInfo:
var info = JsonCovert.DeserializeObject<SortingPagingInfo>(sortData);
Make your links submit buttons instead (you can still style them as links) and have them actually submit a form with all this data:
<form asp-page-handler="./Page" method="get">
#Html.Hidden("SortField", info.SortField)
#Html.Hidden("SortDirection", info.SortDirection)
#Html.Hidden("PageCount", info.PageCount)
#Html.Hidden("PageSize", info.PageSize)
#Html.Hidden("CurrentPageIndex", info.CurrentPageIndex)
<button type="submit" class="btn btn-link">
#Html.DisplayNameFor(model => model.Tabl[0].Col1)
</button>
</form>
However, you currently have a form that wraps your entire table, and you cannot have forms within forms. As a result, you would need to restructure your HTML to ensure that each of these links was outside of any other forms on the page.

How to search data in multiple fields / columns?

I tried a code like this, it works quite well:
View:
#model IEnumerable<InternProject.Models.Course>
....
#Html.ActionLink("Create New", "Create")
#using (Html.BeginForm("Index", "Course", FormMethod.Get))
{
<p>
#Html.TextBox("searching")
<input type="submit" value="search" />
</p>
}
<table class="table table-striped">
<thead>
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Major</th>
<th>Specialization</th>
<th></th>
</tr>
</thead>
#foreach (var item in Model) {
<tr>
<td>#Html.DisplayFor(modelItem => item.crs_ID)</td>
<td>#Html.DisplayFor(modelItem => item.crs_Course)</td>
<td>#Html.DisplayFor(modelItem => item.crs_Major)</td>
<td>#Html.DisplayFor(modelItem => item.crs_Specialization)</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.crs_ID }) |
#Html.ActionLink("Details", "Details", new {id = item.crs_ID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.crs_ID })
</td>
</tr>
}
</table>
Controller:
public class CourseController : Controller
{
private DbCourse db = new DbCourse();
public ActionResult Index(string submit, string searching)
{
var course = from x in db.Course select x;
if (!String.IsNullOrWhiteSpace(searching))
{
return View(db.Course.Where(x => x.crs_Course.Contains(searching.Trim()) ||
x.crs_Major.Contains(searching.Trim()) ||
x.crs_Specialization.Contains(searching.Trim())).ToList());
}
else if (searching == null)
{
return View(db.Course.Where(x => x.crs_Course == searching || searching.Trim() == null).ToList());
}
else
{
return View(db.Course.ToList());
}
}
}
But the id cannot be included because it is an integer. I want to have a solution wherein I can search also in the id of my database depending on the input in the search box.
Also, is there a better code than this for a simple search functionality like this? I've noticed it's so long and it obviously violates the DRY principle.
Here is what my simple application looks like:
I'm taking my baby steps in ASP.NET MVC as a beginner.
I hope to improve my knowledge using applied coding and not just relying on tutorial videos.
Thank you very much in advance! =)
The simplest solution would be to convert the ID to a string. Your code then become the following.
return View(db.Course.Where(x => x.crs_Course.Contains(searching.Trim()) ||
x.crs_Major.Contains(searching.Trim()) ||
x.crs_Specialization.Contains(searching.Trim()) ||
x.crs_crs_ID.ToString().Contains(searching.Trim())).ToList())
This doesn't go against the DRY principle since you're using Contains() on different variables; however, what is going against the DRY principle is the repetitive searching.Trim(). I suggest you trim the string once at the top of your code.
var match = searching.Trim();
Then you can use match instead of searching.Trim() in the code below.

How can I create a list of object based on checkboxfor created for the model

I have this view based on a list of a model where I create strongly-typed checkboxes for each items of the model based on a boolean.
Here's my view:
#using MyApp.Models
#model IList<MyApp.Models.ObjInfo>
#{
ViewBag.Title = "Obj Inventory";
}
<h2>Search Inventory</h2>
<p>
#using (Html.BeginForm())
{
(Many search filters which are non-relevant)
<p>
Send Items: #Html.ActionLink("Click Here", "SendItems")
</p>
}
<table>
<tr>
<th>
Obj Name
</th>
<th>
Number In Stock
</th>
(...)
<th>
Select Item
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.OtherObj.m_Name)
</td>
(...)
<td>
#Html.CheckBoxFor(modelItem => item.m_IsSelected)
</td>
</tr>
}
</table>
The whole process works fine and I can actually generate a view with checkboxes for each item of my list of model.
Now my question is that I want to create a list which would regroup only the items in the list which are checked and send them to the controller. How could I do that? Can anyone help me or suggest me a way to work?
Thank you!
* EDIT *
Here is the HttpPost Method used to get the List of items as mentioned below:
//
// GET: /Inventory/SendItems
[HttpPost]
public ActionResult SendItems(IList<ObjInfo> listToSend)
{
m_ListObjToSend = new List<ObjInfo>();
foreach (var item in listToSend.Where(item => item.m_IsSelected))
{
m_ListObjToSend .Add(item);
}
return View(m_ListObjToSend );
}
However I have encountered many problems:
This method does NOT work if I put the [HttpPost] attribute (it will show as "Not Found");
The list I am supposed to receive is null;
Each hiddenfield linked with the checkbox has default value as false even if the checked value shows true;
I am using an actionlink because I do not want to use a button, there is already one that is doing another job.
I am open for any comments / help available, thank you!
If you use the CheckBoxFor helper to generate checkboxes you will notice that it generates an additional hidden field along with each checkbox. This means that all values will be sent to the controller and you will have to filter in your controller those that are checked.
Also I would recommend you using indexes to ensure proper model binding. You just need to use an IList<ObjInfo> or ObjInfo[] which is trivially easy achievable by calling .ToList() or .ToArray() extension methods on your view model before passing it to the view:
#using MyApp.Models
#model IList<ObjInfo>
...
#for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(x => x[i].OtherObj.m_Name)
</td>
(...)
<td>
#Html.CheckBoxFor(x => x[i].m_IsSelected)
</td>
</tr>
}
...
And now your controller action could directly take the list of items:
[HttpPost]
public ActionResult SomeAction(IEnumerable<ObjInfo> model)
{
...
}
and if you wanted to find the selected values, you could simply get them through LINQ:
[HttpPost]
public ActionResult SomeAction(IEnumerable<ObjInfo> model)
{
var selectedItems = model.Where(x => x.m_IsSelected);
...
}
Remark: m_Name and m_IsSelected is a disastrously bad naming convention for a properties in C#.
UPDATE:
Another issue you have with your code is that your Html.BeginForm doesn't contain any input field. It has only a single ActionLink which obviously only does a GET request. If you want to submit the values you should wrap your entire table with the form and use a submit button and not some action links:
#using MyApp.Models
#model IList<ObjInfo>
#{
ViewBag.Title = "Obj Inventory";
}
<h2>Search Inventory</h2>
<p>
#using (Html.BeginForm("SendItems", null, FormMethod.Post))
{
(Many search filters which are non-relevant)
<table>
<tr>
<th>Obj Name</th>
<th>Number In Stock</th>
(...)
<th>Select Item</th>
</tr>
#for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>
<!--
This will not be sent to your controller because it's only a label.
You will need a corresponding hidden field if you want to get that value back
-->
#Html.DisplayFor(x => x[i].OtherObj.m_Name)
</td>
(...)
<td>
#Html.CheckBoxFor(x => x[i].m_IsSelected)
</td>
</tr>
}
</table>
<p>
Send Items: <button type="submit">Click Here</button>
</p>
}
</p>
So really, 2 things you should learn:
The naming convention that the default model binder expects when binding to a list
How to use a javascript debugging tool (such as FireBug and/or Chrome Developper Toolbar) which will allow you to inspect all the values that are sent to your server and immediately recognized whether you respected the convention you learned in 1.

Categories

Resources