public class Libro
{
public string Titolo { get; set; }
public string Autore { get; set; }
public string Editore { get; set; }
public int ISBN { get; set; }
public int Pagine { get; set; }
public decimal Prezzo { get; set; }
public int Quantità { get; set; }
public Libro BuildLibro(string input)
{
Libro result = null;
if (!String.IsNullOrEmpty(input))
{
var inputArray = input.Split('*');
if (inputArray.Length >= 6)
{
result = new Libro();
result.Titolo = inputArray[0];
result.Autore = inputArray[1];
result.Editore = inputArray[2];
if (!string.IsNullOrEmpty(inputArray[3]))
{
int.TryParse(inputArray[3], out int num);
result.ISBN= num;
}
if (!string.IsNullOrEmpty(inputArray[4]))
{
int.TryParse(inputArray[4], out int num);
result.Pagine = num;
}
if (!string.IsNullOrEmpty(inputArray[5]))
{
decimal.TryParse(inputArray[5], out decimal num);
result.Prezzo = num/100;
}
if (!string.IsNullOrEmpty(inputArray[6]))
{
int.TryParse(inputArray[6], out int num);
result.Quantità = num;
}
}
}
return result;
}
}
}
in the Index.cshtml
<table class="table">
<tr>
<th>
Titolo
</th>
<th>
Autore
</th>
<th>
Editore
</th>
<th>
Prezzo(€)
</th>
</tr>
#foreach (var line in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => line.Titolo)
<p> Visualizza la scheda </p>
</td>
<td>
#Html.DisplayFor(modelItem => line.Autore)
</td>
<td>
#Html.DisplayFor(modelItem => line.Editore)
</td>
<td>
#Html.DisplayFor(modelItem => line.Prezzo)
</td>
</tr>
}
</table>
</body>
</html>
I have created a model of a book based on a list of a file.txt in order to display a table with all the books available, their author, publisher and price, now, for each book, I should be able to open a descriptive sheet also containing ISBN , pages and quantities. From paragraph in the html below #Html.DisplayFor(modelItem => line.Titolo) I inserted a link to the About.cshtml, but I don't know what code to write inside and what to write in the controller too?
this is my controller at the moment:
public ActionResult Index()
{
var fileInput = Reader.Read("C:/Users/test/source/repos/Books/Books/App_Data/Libri.txt");
var libriList = new List<Libro>();
if (fileInput != null)
{
for (var i = 1; i < fileInput.Count; i++)
{
var libri = new Libro();
libri = libri.BuildLibro(fileInput[i]);
if (libri != null)
{
libriList.Add(libri);
}
}
}
Session["currentLibriList"] = libriList;
return View(libriList);
}
public ActionResult About(string titoloId)
{
var myFilteredList = new List<Libro>();
if (Session["currentLibriList"] != null)
{
var lookupList = (List<Libro>)(Session["currentLibriList"]);
myFilteredList = (List<Libro>)lookupList.Where(x => x.Titolo == titoloId);
}
return View(myFilteredList);
}
and this is About.cshtml
<html>
<body>
<table class="table">
#foreach (var line in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => line.Titolo)
</td>
<td>
#Html.DisplayFor(modelItem => line.Autore)
</td>
<td>
#Html.DisplayFor(modelItem => line.Editore)
</td>
<td>
#Html.DisplayFor(modelItem => line.Prezzo)<p>€</p>
</td>
<td>
#Html.DisplayFor(modelItem => line.Pagine)
</td>
<td>
#Html.DisplayFor(modelItem => line.Quantità)
</td>
<td>
#Html.DisplayFor(modelItem => line.ISBN)
</td>
</tr>
}
</table>
</body>
</html>
You can use Url.Action helper to send the line.Titolo to your Controller method and get your data:
<td>
<p><a class="btn btn-default" href="#Url.Action("About","Home", new { titoloId = line.Titolo })">#line.Titolo</a></p>
</td>
Your Index method will store the data in a Session variable and then you retrieve this Session in your About method and cast it to the List<Libro> type. Once you get the list, then you can filter it out based on the titolId. Your last step would be to send this filtered list to your About view. You need to build your About view as per your requirement with the filtered data.
public ActionResult About(string titoloId)
{
var myFilteredList= new List<Libro>();
//Get data for titoloId
if (Session["currentLibriList"] != null)
{
var lookupList = (List<Libro>)(Session["currentLibriList"]);
myFilteredList=lookupList.Where(x=>x.Titolo == titoloId);
}
return View(myFilteredList);
}
public ActionResult Index()
{
var fileInput = Reader.Read("C:/Users/test/source/repos/Books/Books/App_Data/Libri.txt");
var libriList = new List<Libro>();
if (fileInput != null)
{
for (var i = 1; i < fileInput.Count; i++)
{
var libri = new Libro();
libri = libri.BuildLibro(fileInput[i]);
if (libri != null)
{
libriList.Add(libri);
}
}
}
//Set your session here
Session["currentLibriList"]=libriList;
return View(libriList);
}
Related
This is the model
I want to get sum of values in Mon,Tue,Wed,Thr,Fri,Sat,Sun and display them on a different page.
Database is connected with local db using add migrations command
namespace TimeSheat.Models
{
public class Timesheet
{
public int Id { get; set; }
public int UserID { get; set; }
public string Project { get; set; }
public string Activity { get; set; }
public string DeptCode { get; set; }
public int Mon { get; set; }
public int Tue { get; set; }
public int Wed { get; set; }
public int Thr { get; set; }
public int Fri { get; set; }
public int Sat { get; set; }
public int Sun { get; set; }
public Timesheet()
{
}
}
}
These are some controller methods if needed
public async Task<IActionResult> ShowSearch()
{
return View(await _context.Timesheet.ToListAsync());
}
// GET: Timesheets/ShowSearchResults
public async Task<IActionResult> ShowSearchResults(string SearchPhrase)
{
return View("Index", await _context.Timesheet.Where(j => j.Project.Contains(SearchPhrase)).ToListAsync());
}
// GET: Timesheets/ShowSummary
public async Task<IActionResult> ShowSummary()
{
return View(await _context.Timesheet.ToListAsync());
}
// GET: Timesheets/ShowSummary
public async Task<IActionResult> ShowSummaryResults(int SearchPhrase)
{
var a = View("Index", await _context.Timesheet.Where(j => j.UserID.Equals(SearchPhrase)).ToListAsync());
return a;
}
Using # like below
#(item.Mon + item.Tue + item.Wed + item.Thr + item.Fri + item.Sat + item.Sun)
Codes of sample view
#model IList<TimeSheat.Models.Timesheet>
#{
ViewData["Title"] = "Summary";
}
<h1>Summary</h1>
<table class="table">
<thead>
<tr>
<td>Id</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thr</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
<td>
Total
</td>
</tr>
</thead>
<tbody>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.Mon)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tue)
</td>
<td>
#Html.DisplayFor(modelItem => item.Wed)
</td>
<td>
#Html.DisplayFor(modelItem => item.Thr)
</td>
<td>
#Html.DisplayFor(modelItem => item.Fri)
</td>
<td>
#Html.DisplayFor(modelItem => item.Sat)
</td>
<td>
#Html.DisplayFor(modelItem => item.Sun)
</td>
<td>
#(item.Mon + item.Tue + item.Wed + item.Thr + item.Fri + item.Sat + item.Sun)
</td>
</tr>
}
</tbody>
</table>
Screenshot
I have 4 SQL Tables with similar data fields. The reason for their separation is because they contain data from different locations. I have implemented a search box that is able to pull and display the records from these tables. However, there is a fourth table with totally different fields I have tried implementing the same on it but with no luck.
I tried creating a table within the same if function but inside a different #foreach given that they reside in the same model class
public class MemberViewModel
{
public string Client { get; set; }
public string PolicyNo { get; set; }
public Nullable<short> PolicyType { get; set; }
public string InsurerName { get; set; }
public Nullable<System.DateTime> RenewalDate { get; set; }
public string Status { get; set; }
public string Telephone { get; set; }
}
public class DisplayData
{
public List<MemberViewModel> Search(List<string> keywords)
{
StringBuilder sqlbuilder = new StringBuilder();
sqlbuilder.Append(" select * from SingleView where");
foreach (string item in keywords)
{
sqlbuilder.AppendFormat("([Telephone] like '%{0}%' or [Client] like '%{0}%' or [PolicyNo] like '%{0}%' or [PolicyType] like '%{0}%') and ", item);
}
string sql = sqlbuilder.ToString(0, sqlbuilder.Length - 5);
return QueryList(sql);
}
This is the View
#if (ViewBag.Message == true)
{
<label id="lblMessage" style="color: #6fcdcd;">Enter Phone Number</label>
}
else
{
if (Model != null)
{
if (Model.Count() != 0)
{
<div class="container">
<table id="example" class="table table-bordered table-hover table-responsive">
<thead class="thead-dark">
<tr>
<th>Client</th>
<th>Policy No</th>
<th>Policy Type</th>
<th>Insurer Name</th>
<th>Renewal Date</th>
<th>Status</th>
<th>Telephone</th>
</tr>
</thead>
#foreach (var item in Model.OrderByDescending(m => m.Client))
{
if (item.Status == "CURRENT")
{
#:
<tr style="background-color:#13994a ;text-align:center;font-family:Calibri">
<td>
#Html.DisplayFor(modelitem => item.Client)
</td>
<td>
#Html.DisplayFor(modelitem => item.PolicyNo)
</td>
<td>
#Html.DisplayFor(modelitem => item.PolicyType)
</td>
<td>
#Html.DisplayFor(modelitem => item.InsurerName)
</td>
<td>
#Html.DisplayFor(modelitem => item.RenewalDate)
</td>
<td>
#Html.DisplayFor(modelitem => item.Status)
</td>
you can use some list with object type and then use it for every table as you want for example:
List<object> l = new List<object>();
l.Add("sssss");
l.Add(1);
l.Add(new SomeObject()
{
Id = 1,
LastNane = 23,
Name = 1,
RegDate = DateTime.Now
});
I want to filter data when user check multiple checkbox and show data from this View (1) to another View (2)
I tried using Model and make loop in Controller (2) to get value checbox when user check multiple checbox,
I have a problem with loop in controller when return... I can't return multiple data when user check multiple checkbox.
It only returns the data of the last loop
Controller
ContainerTrackingSystemEntities db = new ContainerTrackingSystemEntities();
BaseModel modelResult = new BaseModel();
BaseModel model = new BaseModel();
// GET: CurrentContainerReport
public ActionResult Index()
{
//if (Session["User"] == null)
//{
// SetAlert("Vui lòng đăng nhập", "info");
// return RedirectToAction("LoginUsers", "Users");
//}
ViewBag.PortCode = db.APS_Inventory_Port.OrderBy(n => n.Code);
//model.aPS_Inventory_Containers = (from c in db.APS_Inventory_Container select c).GroupBy(n => n.Size).Select(n => n.FirstOrDefault()).Distinct().ToList();
model.aPS_Inventory_OwnerAgentss = (from o in db.APS_Inventory_OwnerAgent select o).OrderBy(n => n.Code).ToList();
model.aPS_Inventory_Portss = (from p in db.APS_Inventory_Port select p).OrderBy(n => n.Code).ToList();
model.aPS_Inventory_Containerss = (from c in db.APS_Inventory_Container select c).GroupBy(n => n.Size).Select(n => n.FirstOrDefault()).OrderBy(n => n.Size).ToList();
return View(model);
}
[HttpPost]
public ActionResult Index(BaseModel model)
{
TempData["model"] = model;
return RedirectToAction("Search");
}
[HttpGet]
public ActionResult Search(BaseModel model)
{
model = (BaseModel) TempData["model"];
List<APS_Inventory_Container> aPS_Inventory_Containers = new List<APS_Inventory_Container>();
for (int i = 0; i < model.aPS_Inventory_OwnerAgentss.Where(n => n.IsCheckedOwnerAgent == true).Count(); i++)
{
bool isChecked = model.aPS_Inventory_OwnerAgentss[i].IsCheckedOwnerAgent;
Guid? OwnerID = model.aPS_Inventory_OwnerAgentss[i].OwnerAgentID;
aPS_Inventory_Containers = (from c in db.APS_Inventory_Container
where c.PortCode == model.aps_inventory_port.Code && c.OwnerAgentID == OwnerID
select c).ToList();
}
return View(aPS_Inventory_Containers);
}
View Index
#for (int i = 0; i < Model.aPS_Inventory_OwnerAgentss.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(item => item.aPS_Inventory_OwnerAgentss[i].Code)
</td>
<td>
#Html.CheckBoxFor(item => item.aPS_Inventory_OwnerAgentss[i].IsCheckedOwnerAgent, new { #checked = "checked" })
#Html.HiddenFor(item => item.aPS_Inventory_OwnerAgentss[i].OwnerAgentID)
#Html.HiddenFor(item => item.aPS_Inventory_OwnerAgentss[i].Code)
</td>
</tr>
}
View Search
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(itemSelect => item.Code)
</td>
<td>
#Html.DisplayFor(itemSelect => item.Status)
</td>
<td>
#Html.DisplayFor(itemSelect => item.PortCode)
</td>
<td>
#Html.DisplayFor(itemSelect => item.Size)
</td>
<td>
#Html.DisplayFor(itemSelect => item.Type)
</td>
<td>
#Html.DisplayFor(itemSelect => item.DateofManufactured)
</td>
<td>
#Html.DisplayFor(itemSelect => item.OwnerAgentID)
</td>
<td>
#Html.DisplayFor(itemSelect => item.Comments)
</td>
<td>
#Html.DisplayFor(itemSelect => item.approve)
</td>
</tr>
}
And Base Model
public class BaseModel
{
public APS_Inventory_Bill aps_inventory_bill { get; set; }
public APS_Inventory_BillDetails aps_inventory_billdetails { get; set; }
public APS_Inventory_Container aps_inventory_container { get; set; }
public APS_Inventory_Depot aps_inventory_depot { get; set; }
public APS_Inventory_Port aps_inventory_port { get; set; }
public APS_Inventory_OwnerAgent aps_inventory_owneragent { get; set; }
public List<APS_Inventory_Container> aPS_Inventory_Containerss { get; set; }
public List<APS_Inventory_OwnerAgent> aPS_Inventory_OwnerAgentss { get; set; }
public List<APS_Inventory_Port> aPS_Inventory_Portss { get; set; }
public List<APS_Inventory_Bill> aPS_Inventory_Billss { get; set; }
public List<APS_Inventory_BillDetails> aPS_Inventory_BillDetailss { get; set; }
}
And if anyone has another way please let me know
Thanks !
With the line aPS_Inventory_Containers = .... you overwrite the value of aPS_Inventory_Containers every time you run the loop. In other words, you replace its previous value with a new value. So after the loop finishes, there's only the last value remaining - you have destroyed all the others. You probably want to add items to the list, instead of replacing the whole list each time.
Try this instead:
aPS_Inventory_Containers.AddRange(
(from c in db.APS_Inventory_Container
where c.PortCode == model.aps_inventory_port.Code && c.OwnerAgentID == OwnerID
select c).ToList()
);
i have a List of Start Time and Time and i only want Time but it show with date
Model
public DateTime Starttime { get; set; }
public DateTime EndTime { get; set; }
public int Status { get; set; }
Viewe
#foreach (var item in Model)
{
if (item != null)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Starttime)
</td>
<td>
#Html.DisplayFor(modelItem => item.EndTime)
</td>
#if (item.Status == 1)
{
<td>
<p>Avaliable</p>
</td>
}
#if (item.Status == 2)
{
<td>
<p>Cancel</p>
</td>
}
</tr>
}
}
its show me this result
OutPut
But i need this format it is possible ??
i want this
its my First question on StatkOverFlow
Here is the definition of CourseRegisterModel:
public class CourseRegisterModel
{
public StudentModel Student { get; set; }
public CourseModel Course { get; set; }
public CourseModel Course1 { get; set; }
public CourseModel Course2 { get; set; }
public CourseModel Course3 { get; set; }
public CourseModel Course4 { get; set; }
public CourseModel Course5 { get; set; }
public List<SelectListItem> CoursesList { get; set; }
public DateTime RegisterDate { get; set; }
}
Here is my controller function which executes on Home Page load:
public ActionResult Home()
{
//Retrieve all registered courses for this bloody student
ServiceCourseClient client = new ServiceCourseClient();
Course[] coursespending;
var loggedInRollNumber = Request.Cookies["RollNumber"].Value;
coursespending = client.GetPendingRegisteredCourses(loggedInRollNumber.ToString());
List<CourseRegisterModel> coursesRegisteredmodelList = new List<CourseRegisterModel>();
CourseRegisterModel CRM = null;
int i = 0;
foreach (var serviceCourseRegistered in coursespending)
{
CRM = new CourseRegisterModel();
if (i == 0)
{
CRM.Course1.Code = serviceCourseRegistered.Code;
CRM.Course1.Name = serviceCourseRegistered.Name;
}
else if (i == 1)
{
CRM.Course2.Code = serviceCourseRegistered.Code;
CRM.Course2.Name = serviceCourseRegistered.Name;
}
else if (i == 2)
{
CRM.Course3.Code = serviceCourseRegistered.Code;
CRM.Course3.Name = serviceCourseRegistered.Name;
}
else if (i == 3)
{
CRM.Course4.Code = serviceCourseRegistered.Code;
CRM.Course4.Name = serviceCourseRegistered.Name;
}
else if (i == 4)
{
CRM.Course5.Code = serviceCourseRegistered.Code;
CRM.Course5.Name = serviceCourseRegistered.Name;
}
i++;
coursesRegisteredmodelList.Add(CRM);
}
return View(coursesRegisteredmodelList);
}
And here is the view on which i am trying to display registered courses:
#model StudentRegistrationPortal.Models.CourseRegisterModel
#{
ViewBag.Title = "Welcome Student";
}
<h2>Welcome
#Context.User.Identity.Name
</h2>
#Html.ActionLink("[Sign Out]", "SignOut", "Student")
<ul>
<li>#Html.ActionLink("Register Courses", "registerCourses", "Course")</li>
</ul>
<table>
<tr>
<th>
RollNumber
</th>
<th>
Course Code
</th>
<th>
Course Name
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => item)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
But foreach loop on above view is giving following compile time error:
foreach statement cannot operate on variables type CourseRegistrationModel beacuse it does not contain a public definition for GetEnumerator
Please help.
Your model needs to be an IEnumerable. See below.
#model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel>
#{
ViewBag.Title = "Welcome Student";
}
<h2>Welcome
#Context.User.Identity.Name
</h2>
#Html.ActionLink("[Sign Out]", "SignOut", "Student")
<ul>
<li>#Html.ActionLink("Register Courses", "registerCourses", "Course")</li>
</ul>
<table>
<tr>
<th>
RollNumber
</th>
<th>
Course Code
</th>
<th>
Course Name
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => item)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
You were trying to do
foreach(CourseRegisterModel item in CourseRegisterModel)
{
...
}
which is not possible, you want.
foreach(CourseRegisterModel item in IEnumerable<CourseRegisterModel>)
{
...
}
You need to make your Model a List or IEnumerable. You simply pass down the class which does not implement GetEnumerator, it should be:
#model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel>
Or:
#model List<StudentRegistrationPortal.Models.CourseRegisterModel>
Your view model is a single object as shown here:
#model StudentRegistrationPortal.Models.CourseRegisterModel
you may be wanting to iterate over the CoursesList property in your foreach loop:
#foreach (var item in Model.CoursesList) { .... }