I have a ViewModel in: RolesMVC3.Area.Asesor.Models.ListNotesViewModel
is the following:
public class ListNotesViewModel
{
public decimal IdTime { get; set; }
public decimal IdArea { get; set; }
public decimal IdCriterion { get; set; }
public decimal Notes { get; set; }
public decimal IdEstudent { get; set; }
}
I have a controller that uses this ViewModel.
is the following:
public ActionResult EstudentsQualification()
{
var newItems = (from n in db.Qualification
join a in db.AREA on n.IdArea equals a.IdArea
join e in db.ESTUDENT on n.IdEstudent equals e.IdEstudent
join p in db.TIME on n.IdTime equals p.IdTime
join c in db.CRITERION on n.IdCriterion equals c.IdCriterion
where n.IdArea == 1
select new ListNotesViewModel { IdCriterion = c.IdCriterion, IdTime = p.IdTime, Notes=n.Note, IdEstudent==e.IdEstudent }).ToList();
var estu = (from n in db.Qualification
join e in db.ESTUDENT on n.IdEstudent equals e.IdEstudent
where n.IdArea == 1
select e).Distinct().ToList();
ViewBag.Estudents = estu;
ViewBag.Time = db.TIME;
ViewBag.Criterion = db.CRITERION;
ViewBag.Notes = newItems;
return View();
}
the associated view is:
#{
ViewBag.Title = "Index";
}
<table border="1">
#foreach (var item4 in ViewBag.Estudents)
{
<tr>
<td>
#item4.CodEstudents - #item4.NameEstudents
</td>
#foreach (var item2 in ViewBag.Time)
{
foreach (var item3 in ViewBag.Criterion)
{
<td>
#if (ViewBag.Notes.IdCriterion == item3.IdCriterion && ViewBag.Notes.IdTime == item2.IdTime && ViewBag.Notes.IdEstudent == item4.IdEstudent)
{
#ViewBag.Notes.Note
}
else
{
#:nothing
}
</td>
}
}
</tr>
}
</table>
I get the following error:
'System.Collections.Generic.List<RolesMVC3.Area.Asesor.Models.ListNotesViewModel>' does not contain a definition for 'IdCriterion'
I can't figure out what the problem is.
Think this line caused it ViewBag.Notes.IdCriterion
#if (ViewBag.Notes.IdCriterion == item3.IdCriterion && ViewBag.Notes.IdTime == item2.IdTime && ViewBag.Notes.IdEstudent == item4.IdEstudent)
ViewBag.Notes is a collection of Note. You need to access item in Notes e.g. ViewBag.Notes[0].IdCriterion or ViewBag.Notes[i].IdCriterion or foreach(noteItem in ViewBag.Notes)
Related
ASP.NET Core 6 MVC / EF model-first
Is there simple analogue of #Html.DropDownList and ViewBag to replace IDs from one model to values from another model?
I have got two models:
Model1
public int ID { get; set; } // PK
public int PersonID { get; set; } // FK
ID
PersonID
1
3
2
4
Model2
public int PersonID { get; set; } // PK
public int Name { get; set; }
PersonID
Name
3
Nick
4
James
Controller:
public async Task<IActionResult> Index()
{
return View(await _context.Model1.ToListAsync());
}
View:
#model IEnumerable<wpg.monster.Models.Model1>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.PersonID)
</td>
</tr>
}
So I'll get
TRHead
TRHead
1
3
2
4
Is there any trick to replace 3 and 4 with Names from Model2, like ViewBag with #Html.DropDownList to see something like this in View? Or there is only one way to create ViewModel?
TRHead
TRHead
1
Nick
2
James
Here a possible method for you:
First create a model3 to collect the information from model1 and model2:
public class Model1
{
public int ID { get; set; }
public int PersonId { get; set; }
}
public class Model2
{
public int PersonId { get; set; }
public string Name { get; set; }
}
public class Model3
{
public int ID { get; set; }
public string Name { get; set; }
}
Then in your controller, you can select the data you want and insert them to the new model which will be displayed on the viewer, here you can combine two elements whose PersonId are the same to the new model3:
public List<Model3> GetModel3()
{
List<Model1> model1 = new();
List<Model2> model2 = new();
List<Model3> model3 = new();
model1.Add(new Model1 { ID = 1, PersonId = 3 });
model1.Add(new Model1 { ID = 2, PersonId = 4 });
model2.Add(new Model2 { PersonId = 3, Name = "Nick" });
model2.Add(new Model2 { PersonId = 4, Name = "James" });
foreach (Model1 m1 in model1)
{
foreach (Model2 m2 in model2)
{
if(m1.PersonId == m2.PersonId)
{ model3.Add(new Model3 { ID = m1.ID, Name = m2.Name }); }
}
}
return model3;
}
Then you should use the following method in controller to pass the new model to the viewer:
public IActionResult Test()
{
ViewBag.Message = "This is test demo";
dynamic model = new ExpandoObject();
model.Model3 = GetModel3();
return View(model);
}
Last is the viewer page:
#using Test;
#using Test.Models;
#model dynamic
#{
ViewBag.Title = "Test";
ViewData["Title"] = "Test";
}
<h2>#ViewBag.Message</h2>
<table>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
#foreach (Model3 model3 in Model.Model3)
{
<tr>
<td>#model3.ID</td>
<td>#model3.Name</td>
</tr>
}
</table>
Now you may solve your issue, here is my test to it:
I am using a razor view for showing the scored of an examination and it contains an enum value that holds 3 values "pass","Fail","absent" and i want to choose it accordingly. The model i used is
model
public class VerifyResultModel
{
[Display(Name = "StudnetId")]
public int StudentId { get; set; }
[Display(Name = "Student")]
[Required]
public string Student { get; set; }
[Display(Name = "Mark")]
[Required]
public int Mark { get; set; }
[Display(Name = "Score")]
[Required]
public string Score { get; set; }
[Display(Name = "Result")]
public App.EnumValues.ExamResultStatus Result { get; set; }
}
Controller
[HttpGet]
public ActionResult Verify(int Id)
{
List<VerifyResultModel> model_result = new List<VerifyResultModel>();
VerifyResultModel _resultItem;
foreach (exammark item in marks)
{
SchoolApp.EnumValues.ExamResultStatus result = SchoolApp.EnumValues.ExamResultStatus.Absent;
if(item.Mark >= MinMark)
{
result= SchoolApp.EnumValues.ExamResultStatus.Pass;
}
else
{
result = App.EnumValues.ExamResultStatus.Fail;
}
_resultItem = new VerifyResultModel { StudentId = (int)item.StudentId, Student = item.studentmaster.StudentName, Mark = (int)item.Mark, Score = item.Mark.ToString(), Result = result };
model_result.Add(_resultItem);
}
LoadResultsDropdown();
return View(model_result);
}
private void LoadResultsDropdown()
{
var types = (from App.EnumValues.ExamResultStatus type in Enum.GetValues(typeof(SchoolApp.EnumValues.ExamResultStatus))
select new { Id = type.ToString(), Name = type.ToString() }).ToList();
ViewBag.ResultList = new SelectList(types, "Id", "Name");
}
View
#model IList<SchoolApp.ViewModels.VerifyResultModel>
<tbody>
#for (int item = 0; item < Model.Count(); item++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[item].Student)
</td>
<td>
#Html.DisplayFor(modelItem => Model[item].Mark)
</td>*#
<td>
#Html.DisplayFor(modelItem => Model[item].Score)
</td>
<td>
#Html.DropDownListFor(model => model[item].Result, (SelectList)ViewBag.ResultList) //Here All values are showing as Pass ( first item in dropdown)
</td>
</tr>
}
</tbody>
The problem is even if i pass values Fail / Absent to enum it shows as Pass in combobox . How can i show the correct value ?
Can you please try this:
private void LoadResultsDropdown()
{
var types = (from App.EnumValues.ExamResultStatus type in Enum.GetValues(typeof(SchoolApp.EnumValues.ExamResultStatus))
select new { Id = type.ToString(), Name = type.ToString() }).ToList();
var types=Enum.GetValues(typeof(ExamResultStatus)).Cast<ExamResultStatus>();
var EnumData= types.Select(c => new { c.Key, c.Value });
ViewBag.ResultList = new SelectList(types.AsEnumerable(), "key", "value");
}
Hope this helps..
Using EF code first, I have the following 4 entities
public class Item {
public int Id { get; set; }
public string Name { get; set; }
}
public class Location {
public int Id { get; set; }
public string Name { get; set; }
}
public class InventoryAdjustment {
public int Id { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<AdjustmentLine> Lines { get; set; }
}
public class AdjustmentLine {
public int Id { get; set; }
public virtual Item Item { get; set; }
public int Quantity { get; set; }
}
What I am trying to do is to get the sum of all inventory adjustments for each item at each location using minimal database round-trips.
The best I achieved so far is:
using (var db = new InventoryContext()) {
var items = db.Items.ToList();
var locations = db.Locations.ToList();
foreach (var item in items) {
Console.WriteLine(item.Name+":");
foreach (var location in locations) {
Console.Write("\t" + location.Name + ": ");
var qty = db.InventoryAdjustments
.Where(p => p.Location.Id == location.Id)
.SelectMany(p => p.Lines)
.Where(p => p.Item.Id == item.Id)
.Select(p => (int?)p.Quantity)
.Sum();
Console.WriteLine(qty ?? 0);
}
}
Console.Read();
}
The above outputs:
Item1:
Location1: 2
Location2: 12
Location3: 21
Item2:
Location1: 4
Location2: 0
Location3: 0
Item3:
Location1: 1
Location2: 17
Location3: 0
But with 3 items and 3 locations in the database, the above code causes 11 calls to the database. 2 for getting items and locations, and 9 for calculating the sum of quantity.
Is there a better way to get the sum with the least amount of round-trips?
This should probably work:
using (var db = new InventoryContext())
{
var items = db.Items.ToList();
var locations = db.Locations.ToList();
items
.Select(item =>
{
Console.WriteLine(item.Name + ":");
return item;
})
.SelectMany(item => locations.Select(location => new { item, location }))
.GroupJoin(
db.InventoryAdjustments
.SelectMany(
inventoryAdjustment => inventoryAdjustment.Lines.Select(
adjustmentLine => new { key = new { locationId = inventoryAdjustment.Location.Id, itemId = adjustmentLine.Item.Id }, adjustmentLine.Quantity }
)
),
x => new { locationId = x.location.Id, itemId = x.item.Id },
y => y.key,
(x, y) =>
{
Console.WriteLine("\t {0}: {1}", x.location.Name, y.Sum(a => a.Quantity));
return 0;
}
).ToList();
Console.Write("\nPress any key...");
Console.ReadKey();
}
I am new to MVC3, so apologies if this is basic, but I couldn't work it out.
I have a view model that includes 3 tables, an 'Albums' table with 2 foreign keys (Artist ID and Label ID).
I have a 'Labels' controller and a Details method where I display the Label table fields. I am trying to get the distinct 'Artists' from the collection of Albums related to the Label.
At the moment, I can get the name of the artists, but one is generated for each album - I have included the code for this scenario below.
I have tried a bunch of different things, like including Artists in the collection and using the Distinct and group by functions, but to no avail. Not sure if it is doable this way, or whether, due to the indirect relationship between the tables, I need to use a different approach.
Any helps is much appreciated.
Controller:
public ActionResult Details(int id)
{
var viewModel = new LabelsDetailsVM();
viewModel.Lables = db.Labels
.Include(a => a.Albums)
.SingleOrDefault(x => x.LabelID == id);
return View(viewModel);
View:
#foreach (var artist in Model.Lables.Albums)
{
<tr>
<td>
#Html.DisplayFor(model => artist.Artist.ArtistName)
</td>
</tr>
}
View Model
public class LabelsDetailsVM
{
public Label Lables { get; set; }
public IEnumerable<Album> Albums { get; set; }
public IEnumerable<Artist> Artists { get; set; }
}
}
Here's an example that might get you on the right track:
view model:
public class LabelViewModel
{
public Label Label { get; set; }
public IEnumerable<Artist> Artists { get; set; }
}
Controller:
public class LabelController : Controller
{
public ActionResult Details(int id)
{
var label = db.Labels
.Include(l => l.Albums)
.SingleOrDefault(l => l.LabelID == id);
if (label == null)
{
return HttpNotFound();
}
var distinctArtists = label
.Albums
.Select(a => a.Artist)
.Distinct(ArtistComparer.Default);
var model = new LabelViewModel
{
Label = label,
Artists = distinctArtists
};
return View(model);
}
}
and the equality comparer used to distinguish between 2 artists used in the controller that could of course be adapted to match your requirements. In this example it considers that 2 artists represent the same entity if they have the same ID. But you could work with some other properties such as the name and so on, all depends on what you need:
public class ArtistComparer : IEqualityComparer<Artist>
{
protected ArtistComparer()
{
}
private static readonly IEqualityComparer<Artist> _default = new ArtistComparer();
public static IEqualityComparer<Artist> Default
{
get
{
return _default;
}
}
public bool Equals(Artist x, Artist y)
{
if (x != null && y != null)
{
return x.ArtistID.Equals(y.ArtistID);
}
return false;
}
public int GetHashCode(Artist obj)
{
return obj.ArtistID.GetHashCode();
}
}
View:
#model LabelViewModel
<h3>#Html.DisplayFor(x => x.Label.LabelName)</h3>
<div>Artists</div>
<table>
<thead>
<tr>
<th>artist name</th>
</tr>
</thead>
<tbody>
#foreach (var artist in Model.Artists)
{
<tr>
<td>#artist.ArtistName</td>
</tr>
}
</tbody>
</table>
I’m building MVC3 “code first” application and to display data on my page I’m using the following:
Controller Code:
public ViewResult Index()
{
dynamic assignedT = from t in db.AssignedTasks
join a in db.Approvers on t.ApproverID equals a.ID
join r in db.Requestors on t.RequestorID equals r.ID
select new {Approver =a.FirstName + " " +a.LastName,
Requestor=r.FirstName + " " + r.LastName,
Title = t.Title,
t.RequestedDate,
t.CompletedDate, t.Description,
Status =(int)t.InternalStatut
};
return View(assignedT);
}
and on the page:
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
#foreach (var item in Model) {
<tr>
<td>#item.GetType().GetProperty("Title").GetValue(item, null)
</td>
</tr>
}
</table>
I know that using dynamic is not the best way.
How to achieve same functionality using strongly-typed view?
Thank you
You can define the following class:
public class MyViewModel
{
string Approver { get; set; }
string Requestor { get; set; }
string Title { get; set; }
DateTime RequestedDate { get; set; }
DateTime CompletedDate { get; set; }
string Description { get; set; }
int Status { get; set; }
}
If you then change select new in your Linq query to select new MyViewModel then you have a strongly typed viewmodel (and dynamic to var).