I am using treetable.js. as procedure i saved the child id and parent id. insert data is successfully inserted. but return data may show in table only or get the grouping error.
then i use the IGrouping Object get the Convertion error.
Please tell me the child parent loop structure for the below.
In Model:
public string name {get;set;}
public int childId {get;set;}
public int ParentId {get;set;}
In Controller:
var list = db.table.groupby(s=>s.parentId).toList();
return view(list);
In view:
<table id="example-basic">
<tr>
<th>Name</th>
<th>Data Id</th>
<th>Parent Id</th>
</tr>
#foreach (var item in Model)
{
<tr data-tt-id='#item.dataid' data-tt-parent-id='#item.dataparentid'>
<td>#item.Name</td>
<td>#item.dataid</td>
<td>#item.dataparentid</td>
</tr>
}
</table>
If you want just get rid of exception with grouping you should write like this:
var list = db.table.groupby(s => s.parentId).Select(x => new ChartOfAccount
{
ParentId = x.Key,
childId = x.First().childId,
name = x.First().name
}).toList();
return view(list);
As you can see you will get only one row for each ParentId becouse or grouping.
But i gess you searching for something else.
Related
This might be a duplicate question but I can't for the life of me know what the solution is. I'm trying to load a partial view with a table based on the ID of a clicked link.
I'm getting this error:
System.InvalidOperationException: 'The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[MasigasigTrackingSystem.Models.Invoice]', but this ViewDataDictionary instance requires a model item of type 'MasigasigTrackingSystem.Pages.Masigasig.MainPage.TripTicket.TripTicketListModel'.'
I can't seem to figure out how should I passed the model to the partial view.
Here is what my OnGet looks like:
public IList<Invoice> Invoice { get; set; }
public PartialViewResult OnGetDetails(int TripTicketID)
{
var invoices = from m in _context.Invoice
select m;
invoices = invoices.Where(x => x.TripTicketID == TripTicketID);
Invoice = invoices.ToList();
return Partial("_TripTicketListDetails", Invoice);
}
Here is my partial view:
#model TripTicketListModel
<h1>Hello, test</h1>
<table id="TripEntryList" class="table table-bordered table-hover">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Invoice[0].InvoiceNumber)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Invoice)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.InvoiceNumber)
</td>
</tr>
}
</tbody>
You Partial View Required TripTicketListModel and you are passing the model Invoice. It means you are passing wrong model.
From your View code i can say your Invoice is used inside TripTicketListModel, so try below solution.
public PartialViewResult OnGetDetails(int TripTicketID)
{
var invoices = from m in _context.Invoice select m;
invoices = invoices.Where(x => x.TripTicketID == TripTicketID);
TripTicketListModel model = new TripTicketListModel();
model.Invoice = invoices.ToList();
return Partial("_TripTicketListDetails", model);
}
I got this error
The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[CandidateScreening.Data.Entities.Patient]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1[CandidateScreening.Models.Patient]'.)
public ActionResult Index()
{
var Patient = _context.Patients.ToList();
return View(Patient);
}
#model IEnumerable<CandidateScreening.Models.Patient>
#{
ViewBag.Title = "index";
}
<h2>List Of the Patient</h2>
<table class="table">
<thead>
<tr>
<th>firstName</th>
<th>SurName</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td> #Html.ActionLink(item.Firstname, "Index", "detail")</td>
<td> #Html.ActionLink(item.Surname, "Index", "detail")</td>
<td> #Html.ActionLink(item.Gender, "Index", "detail")</td>
</tr>
}
</tbody>
</table>
could you tell me why I get this error?
I have try by changing IEnumerable to List but is not working yet
Assumed Patients is an Entity Framework data context, ToList() will generate list with object type set as IEnumerable<CandidateScreening.Data.Entities.Patient>, which doesn't match with object type set by #model directive (i.e. IEnumerable<CandidateScreening.Models.Patient>).
To solve this issue, simply use Select() LINQ query to project entity context into list of viewmodel class:
public ActionResult Index()
{
var Patient = _context.Patients.Select(x => new CandidateScreening.Models.Patient
{
// list of properties here
// example:
// PatientId = x.PatientId
}).ToList();
return View(Patient);
}
Or using query expression as alternative:
var Patient = (from patient in _context.Patients
select new CandidateScreening.Models.Patient
{
// list of properties here
// example:
// PatientId = patient.PatientId
}).ToList();
I have a view that displays user names with groups they are part of.
In case i switch the group of a user, how can i send bot the user and the new group to the controller ?
View :
<table>
<tr>
<td>user ID</td>
<td>user group</td>
<td>Actions</td>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#item.UserName</td>
<td>#Html.DropDownList("grup", new SelectList(ViewBag.GroupList, "ID", "UserGroupName"), item.UserGroupName)</td>
<td>#Html.ActionLink("Save","EditUserGroup", UserInGroup)</td>
</tr>
}
</table>
Viewbag.GroupList is a list of {ID, GroupName}
Controller
public ActionResult EditUserGroup(UserInGroup userInGroup, string grup)
{
}
How can i send the value of the selected group and the user with all the details ?
Edit
I changed the Model to :
public class UserInGroupModel
{
public IList<UserInGroup> userInGroupList { get; set; }
public int GroupId { get; set; }
}
Controller
public ActionResult UserGroup()
{
IUserGroupService userGroupService = new UserGroupService();
ViewBag.GroupList = userGroupService.GetEntities();
var lista = userInGroupService.GetEntities();
UserInGroupModel userInGroupModel = new UserInGroupModel();
userInGroupModel.userInGroupList = lista;
return View(userInGroupModel);
}
View:
#using (Html.BeginForm("EditUserGroup", "Admin"))
{
<table>
<tr>
<td>user ID</td>
<td>user group</td>
<td>Actions</td>
</tr>
#foreach (var item in Model.userInGroupList)
{
<tr>
<td>#item.UserName</td>
<td>#Html.DropDownListFor(model => model.GroupId, new SelectList(ViewBag.GroupList, "ID", "UserGroupName"))</td>
<td><input type="submit" value="Save"/></td>
</tr>
}
</table>
}
I still cannot send the value of the UserInGroup. I can get the GroupId in the groupId parameter of the controller but, the UserInModel entity is not passed. Can you help me ?
use dropdownlistfor
#Html.DropDownListFor(x => x.grup, new SelectList(ViewBag.GroupList, "ID"...
add a variable to your model for the selected item (grup in this case)
I am just trying to pass a List and display it dynamically in a table in the View. I have a Homepage Model and Homepage controller and the variables are being set right, but I can't figure out how to pass it to the view.
My model looks like this:
public class HomePageModel
{
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "ExtNum")]
public string ExtNum { get; set; }
[Display(Name = "PhoneDisplay")]
public List<PhoneDisplay> PhoneDisplay { get; set; }
}
and this is the controller:
public ActionResult Homepage(HomePageModel HpModel)
{
ViewBag.Welcome = "Welcome: ";
ViewBag.FirstName = HpModel.FirstName;
ViewBag.LastName = HpModel.LastName;
ViewBag.Extlbl = "Extension: ";
ViewBag.Ext = HpModel.ExtNum;
ViewBag.Todaylbl = "Today:";
ViewBag.Today = DateTime.Now;
DBOps ops = new DBOps();
HpModel.PhoneDisplay = ops.getDisplayInfo(HpModel.ExtNum);
return View(HpModel);
}
PhoneDisplay is a list that contains a line index, a description string and a 4 digit number. Each user will have at least 1 item in this list and maximum 6. I was able to pass the other parameters and display them in the view but I can't find a way to pass the list and display that dynamically.
EDIT
I made it this far but still can't find the list items.
#model AxlMVC.Models.HomePageModel
<table>
<caption style="font-weight:bold">Your Phone Information</caption>
<tr>
<th>Line Index</th>
<th>Display</th>
<th>Extension Number</th>
</tr>
#{
foreach (var item in Model.PhoneDisplay) //problems here
{
<tr>
<td>
#Html.Display(item.numplanindex)
</td>
<td>
#Html.Display(item.display)
</td>
<td>
#Html.Display(item.dnorpattern)
</td>
</tr>
}
}
</table>
EDIT
I debugged the cshtml file and the items in the foreach loop are being passed just fine too, but the table is not showing on the page and neither are the items all I can see is the caption and the headers for each column
Html.Display displays "data from the ViewData dictionary or from a model" as stated on MSDN. What it means is that it searches for the key in the ViewData dictionary with the value you pass in or a property in the Model with the specified name. E.g. Display("test") would search ViewData for the "test" key and the Model for the property named test. Since you are passing in property values that cannot work. Your options are:
Output the value directly, #item.numplanindex. This will output a string representation of the value.
Use Display, although this is not recommended. You could do Display("PhoneDisplay[1].numplanindex") to display numplanindex property of the second item in list.
Use DisplayFor, like DisplayFor(model => item.numplanindex). This is a strongly typed version of Display. It will either displays a string representation of the value or a template for the type, if you have one. You can also manage how the output is displayed via Data Annotations, e.g. DisplayFormatAttribute.
Use DisplayTextFor, like DisplayTextFor(model => item.numplanindex). This method outputs the string representation of the value.
Since you already have data annotations on the model, you could modify your view like this:
#model AxlMVC.Models.HomePageModel
<table>
<caption class="tableCaption">Your Phone Information</caption>
<tr>
<th>#Html.DisplayNameFor(model => model.PhoneDisplay[0].numplanindex)</th>
<th>#Html.DisplayNameFor(model => model.PhoneDisplay[0].display)</th>
<th>#Html.DisplayNameFor(model => model.PhoneDisplay[0].dnorpattern)</th>
</tr>
#{
foreach (var item in Model.PhoneDisplay)
{
<tr>
<td>#Html.DisplayTextFor(model => item.numplanindex)</td>
<td>#Html.DisplayTextFor(model => item.display)</td>
<td>#Html.DisplayTextFor(model => item.dnorpattern)</td>
</tr>
}
}
</table>
The line #Html.DisplayNameFor(model => model.PhoneDisplay[0].numplanindex) also works if PhoneDisplay contains no items. Only property metadata is collected, expression as such is not executed.
I have a newbie question, which I have tried to understand for the past few days. Hopefully someone can be kind enough to help me understand the programming flow.
Assuming I have a model, the information is stored in the database:
public class Student
{
public int studentID { get; set; }
public string studentName { get; set; }
public strin studentGrade {get; set; }
}
public class StudentDBContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
and I want to display it into the view, with additional checkbox so I can select which students to be promoted into the next grade. I read that one way to do it is by putting into view model:
public class StudentViewModel
{
public bool promoted { get; set; }
public Student stu { get; set; }
}
But I am stuck on is this the way to do it? and if yes, how do you put into the view where it will display all the students with a checkbox next to it. Afterwards, I want to update all the grade for the students whose checkboxes are ticked. For example:
Student A, Student B, Student D promoted from Grade 1 to Grade 2. So I want to display the students, tick Student A, B and D and submit to update the Grade.
Step by step example will be much appreciated.
Update 1:
Controller:
[HttpGet]
public ViewResult CheckBox()
{
var studentViewModels = db.Students.Select(m => new StudentViewModel()
{
stu = m
}).ToList();
return View(studentViewModels);
}
[HttpPost]
public ActionResult CheckBox(IList<studentViewModel> list)
{
foreach (var stuUpdate in list.Where(m => m.promoted))
{
var stuRow = db.Students.Find(stuUpdate.stu.studentID);
stuRow.studentName = stuRow.studentName + "1";
db.Entry(stuRow).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("CheckBox");
}
return RedirectToAction("CheckBox");
}
View:
#model IList<School.ViewModels.StudentViewModel>
#using (Html.BeginForm())
{
<table>
<tr>
<th>
</th>
<th>
student ID
</th>
<th>
student name
</th>
<th>
student grade
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.CheckBoxFor(modelItem => item.promoted)
#Html.HiddenFor(modelItem => item.stu.studentID)
</td>
<td>
#Html.DisplayFor(modelItem => item.stu.studentID)
</td>
<td>
#Html.DisplayFor(modelItem => item.stu.studentName)
</td>
<td>
#Html.DisplayFor(modelItem => item.stu.studentGrade)
</td>
</tr>
}
</table>
<input type="submit" value="save" />
}
However currently hit by the following error:
Value cannot be null.
Parameter name: source
Source Error:
foreach (var stuUpdate in list.Where(m => m.promoted))
A very basic "step by step" (done in SO, so I probably did a few mistakes, but you've got the idea).
You have always a few ways to do these kind of things, so... just really take it as a sample, and find other examples to get other ideas.
well, first, in your controller, you will have a GET action (to see the list).
[HttpGet]
public ViewResult StudentList() {
//retrieve all students. With the Select, we create a new instance of StudentViewModel for each student.
//assuming StudentDbContext being a property of your controller, if it's not, you can instantiate a new one (in a using clause)
var studentViewModels = StudentDbContext.Students
.Select(m => new StudentViewModel() {
stu = m
//we don't say nothing about promoted :
//it will be there as "false" by default, which is probably what we want.
}).ToList();
//now we've got a list of StudentViewModel. This will be the model of our view
return View(studentViewModels);
}
Then we've got a view, StudentList.cshtml
in this view, we will display a table, with a line for each student : the studentId (hidden in this case), the name (display only), the grade (display only), and a checkbox.
We need a for loop (not a foreach) to get fine model binding.
#model IList<StudentViewModel>
#using (Html.BeginForm()) {
<table>
<tr>
<th>Student name</th>
<th>Student grade</th>
<th>Promote</th>
</tr>
#for (var i = 0; i < Model.Count(); i++) {
<tr>
<td>#Html.HiddenFor(m => Model[i].Student.studentID)
#Html.DisplayFor(m => Model[i].Student.studentName)
</td>
<td>#Html.DisplayFor(m => Model[i]Student.studentGrade)</td>
<td>#Html.CheckBoxFor(m => Model[i].promoted)</td>
</tr>
}
</table>
<input type="submit" value="save" />
}
This form will lead to another POST action (same name as the GET one, depending of what you have in your Html.BeginForm)
[HttpPost]
public ActionResult StudentList(IList<StudentViewModel> list) {
//we treat only the lines where checkbox has been checked
foreach (var studentViewModel in list.Where(m => m.promoted) {
var student = StudentDBContext.GetById(studentViewModel.Student.studentID);//get student entity instance from context
student.studentGrade ="<new value>";
StudentDBContext.SaveChanges();//save changes, you must have such a method somewhere.
}
return Action("StudentList");
}
Little detail :
Try to respect some really basic "usual" practices : for example in c#, Properties should begin by an uppercase letter (so StudentGrade, StudentName, Promoted, etc).