I have a model that was auto-generated from my SQL database.
class Organization
{
public Organization()
{
this.ContactTitles = new HashSet<ContactTitle>();
this.OrganizationAddresses = new HashSet<OrganizationAddress>();
this.OrganizationBusinessTypes = new HashSet<OrganizationBusinessType>();
this.OrganizationCountries = new HashSet<OrganizationCountry>();
this.OrganizationEmails = new HashSet<OrganizationEmail>();
this.OrganizationMemberships = new HashSet<OrganizationMembership>();
this.OrganizationNotes = new HashSet<OrganizationNote>();
this.OrganizationPhones = new HashSet<OrganizationPhone>();
this.OrganizationWebsites = new HashSet<OrganizationWebsite>();
this.Contacts = new HashSet<Contact>();
this.OrganizationIndustryCodes = new HashSet<OrganizationIndustryCode>();
}
public int OrganizationID { get; set; }
public string Name { get; set; }
public virtual ICollection<ContactTitle> ContactTitles { get; set; }
public virtual ICollection<OrganizationAddress> OrganizationAddresses { get; set; }
public virtual ICollection<OrganizationBusinessType> OrganizationBusinessTypes { get; set; }
public virtual ICollection<OrganizationCountry> OrganizationCountries { get; set; }
public virtual ICollection<OrganizationEmail> OrganizationEmails { get; set; }
public virtual ICollection<OrganizationMembership> OrganizationMemberships { get; set; }
public virtual ICollection<OrganizationNote> OrganizationNotes { get; set; }
public virtual ICollection<OrganizationPhone> OrganizationPhones { get; set; }
public virtual ICollection<OrganizationWebsite> OrganizationWebsites { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
public virtual ICollection<OrganizationIndustryCode> OrganizationIndustryCodes { get; set; }
}
In my Organization View, on my Index page - it is strongly typed to my Organization Model.
I am trying to displaying the Membership information, on the Organization index page, that I believe should be in the ICollection. Unless I am miss-interpreting what that does.
When I go to put a #Html.DisplayFor(modelItem => item.OrganizationMemberships. to grab the data in the OrganizationMembership table, it does not show up on IntelliSense. I only need to be able to display the data, I don't have to submit any changes with a form.
Since the model is an enumerable type -- #model PagedList.IPagedList<VAGTC.Models.Organization> -- you'll need to iterate through them in your main view:
#foreach (var organization in Model)
{
#Html.DisplayFor(model => organization)
}
Next, create a display template for the class Organization. Under Views/Shared/DisplayTemplates add a view Organization.cshtml:
#model VAGTC.Models.Organization
Now this is the main view that renders your class. Here you can iterate over the membership items:
#foreach (var membership in Model.OrganizationMemberships)
{
#Html.DisplayFor(model => membership)
}
Now again, create a partial view for the OrganizationMembership class by adding OrganizationMembership.cshtml under Views/Shared/DisplayTemplates.
Related
I my ASP.NET MVC Core 3.1 app I have Model that stores professions which looks like this:
public partial class Professions
{
public int ProfessionID { get; set; }
public int? ProfessionGroupID { get; set; }
public string ProfessionTitle { get; set; }
public string ProfessionDescription { get; set; }
}
So each profession belongs to some group of professions, therefore Model for profession groups looks like this:
public partial class ProfessionGroups
{
public ProfessionGroups()
{
Professions = new HashSet<Professions>();
}
public int ProfessionGroupID { get; set; }
public int ProfessionGroupTitle { get; set; }
public string ProfessionGroupDescription { get; set; }
public virtual ICollection<Professions> Professions { get; set; }
}
How can I display list of groups of professions in one columns and list of professions in another column
with links which leads to each profession Details View?
This is how output table in some View should look:
ProfessionGroups
Professions
Physiotherapy
Graduated physiotherapist Physiotherapist (bachelor degree) Physiotherapy technician
Geology
Geological technician Geology engineer
So far I've created ViewModel that suppose to hold needed data:
public class ProfGroupsVM
{
public int ProfessionGroupID { get; set; }
public string ProfessionGroupTitle { get; set; }
public List<int> ProfIDs { get; set; } = new List<int>();
public List<string> ProfTitles { get; set; } = new List<string>();
}
For each ProfessionGroupID it has to populate ProfIDs with list of professions belonging to that group and ProfTitles should hold professions titles.
So in Controller so far I have:
var profGroupsVM= new ProfGroupsVM();
var result = from p in _context.Professions
group p.ProfessionID by p.ProfessionGroupID into g
select new { ProfessionGroupID = g.Key, ProfessionIDs= g.ToList() };
I don't know how to assign this result back to my ViewModel nor how to display desired View.
It's better to change the ProfGroupsVM model to this:
public class ProfGroupsVM
{
public int ProfessionGroupID { get; set; }
public string ProfessionGroupTitle { get; set; }
public List<ProfInfo> Professions { get; set; }
public class ProfInfo
{
public int Id { get; set; }
public string Title { get; set; }
}
}
And to get information use this:
_dbContext.ProfessionGroups.Include(x => x.Professions)
.Select(x => new ProfGroupsVM()
{
ProfessionGroupID = x.ProfessionGroupID,
ProfessionGroupTitle = x.ProfessionGroupTitle,
Professions = x.Professions.Select(p => new ProfGroupsVM.ProfInfo
{
Id = p.ProfessionID,
Title = p.ProfessionTitle
}).ToList()
}).ToList();
And in you view try this to show information(I'm not good in UI :)):
#model List<ProfGroupsVM>
<div>
<ul>
#foreach (var group in Model)
{
<li>#group.ProfessionGroupTitle</li>
<ul>
#foreach (var profession in group.Professions)
{
<li>#profession.Title</li>
}
</ul>
}
</ul>
Or if you stick with your ViewModel for ul part you can put this:
<ul>
#foreach (var idsTitles in item.ProfIDs.Zip(#item.ProfTitles, Tuple.Create))
{
<li><a asp-action="Details" asp-controller="Profession" asp-route-id="#idsTitles.Item1">#idsTitles.Item2</a></li>
}
</ul>
I have to create a notes app for homework and it should contain Notes with a List of Tags as property. My generated Notes object looks like this:
public partial class Note
{
public Note()
{
this.Tags = new HashSet<Tag>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string Author { get; set; }
public string Mail { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
Also I have a generated Tag Object:
public partial class Tag
{
public Tag()
{
this.Notes = new HashSet<Note>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Note> Notes { get; set; }
}
This is done with a many to many relationship, i have no entity for NoteTags but a database table.
Now how do I make it possible for the user to edit the tags for a note?
I tried many things already, for example this but that did not work for me because i would have to edit the generated model.
Idk if this helps to understand my situation, here's my editortemplate tag.cshtml:
#model IEnumerable<CRUDApp.Models.Tag>
#foreach (var item in Model)
{
#Html.EditorFor(modelItem => item.Name)<br />
}
Whenever I post the form in the edit mode, Tags will be null within the Note object.
How can I fetch and insert data at a specific time in one view in mvc razor view? I mean to fill a dropdown list from the database in create view.
I want to fill the following when I add the subject and cheater models.
department list
semester list
standard list
stream list
cheater model:
namespace firstapp.Models
{
public class chepter
{
[ForeignKey("dip_id")]
public int dipart_id { get; set; }
public int chep_id { get; set; }
public string subject { get; set; }
public string chepter { get; set; }
public List<dipartment> dipartlist { get; set; }
public List<dipartment> stdlist { get; set; }
public List<dipartment> semlist { get; set; }
public List<dipartment> stremlist { get; set; }
}
}
department model:
namespace firstapp.Models
{
public class dipartment
{
public int dip_id { get; set; }
public string dipart { get; set; }
public string std { get; set; }
public string sem { get; set; }
public string strem { get; set; }
}
}
#Html.DropDownListFor(model => model.dipart_id, new SelectList(Model.dipartlist.Select(s => new SelectListItem() { Value = s.dip_id, Selected = false, Text = s.dipart })), "Select")
Change your model so the list property is a selectlist:
public SelectList<dipartment> dipartlist { get; set; }
Then, when you populate the model call a service class method(you might not have a service layer, I just prefer to not have database calls in the controller)
dipartlist = _departmentService.GetAsSelectList();
The GetAsSelectList service method looks like this:
public SelectList GetAsSelectList()
{
return (from d in _context.Set<department>().OrderBy(x => x.dipart)
select new
{
Id = d.dipart_id,
Name = d.dipart
}).ToList();
}
And finally your view:
#Html.DropDownListFor(model => model.dipart_id, Model.dipartlist)
This technique means you don't have linq in either the view or controller. Also as you're only creating the selectlist in one place (the service), you can cache it with MemoryCache to prevent multiple requests for the same data. And as it looks like you're populating 4 selectlists, this might be useful.
I have a complex View. It has data from 4 Models. The models are all static and work as expected. I have created a ViewModel to attempt to show just the data needed for this view. It is made up of Competitors and some complex Classes and Events they participate in.
I have made a complex ViewModel. When I walk through the Controller, I can see all three parts being constructed from the ViewModel. Its all there including data. When I try to map the values using Intellesense in the View, it has no way of knowing this data, or has no mapping from the complex ViewModel. Am I doing this right? I have tried several ways to map these values to the View. I think I need to initialize or map the values to the Models derived from, I just cannot figure out how.
Please advise on how to map these values, data elements to the view.
ViewModel:
Compeditor is an from an actual model direct to the DB
The rest of the data is gathered from multiple tables and passed to view from controller
namespace eManager.Web2.Models
{
public class CompDetailPlus
{
public CompDetailPlus()
{
this.Compeditor = new Compeditor();
}
public virtual Compeditor Compeditor { get; set; }
public virtual IEnumerable<InEventClass> InEventClass { get; set; }
public virtual IEnumerable<AllEventClasses> AllEventClasses { get; set; }
}
public class Compeditor
{
[Key]
public virtual int CompeditorId { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string MiddleInt { get; set; }
public virtual string StreetAddress { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string PostalCode { get; set; }
public virtual string EmailAddress { get; set; }
public virtual string HomePhone { get; set; }
public virtual string CellPhone { get; set; }
public virtual double Height { get; set; }
public virtual double Weight { get; set; }
public virtual int Age { get; set; }
public virtual int Event_CompId { get; set; }
}
public class InEventClass
{
public virtual int EventClassID { get; set; }
public virtual string ClassName { get; set; }
public virtual bool IsSelected { get; set; }
}
//duplicate to simplify how the second list is pulled and then combined with first list
public class AllEventClasses
{
public virtual int EventClassID { get; set; }
public virtual string ClassName { get; set; }
public virtual bool IsSelected { get; set; }
}
}
Controller:
public ActionResult CompeditorDetail(int CompeditorId)
{
//Pull the Competitor detail for the ID passed in
var comp = _db.Compeditors.Single(c => c.CompeditorId == CompeditorId);
//Pull a list of Event-Classes the competitor is already signed up for on current event
var nlist = (from o in _db.Compeditors
join o2 in _db.Event_Class_Compeditors_s on o.CompeditorId equals CompeditorId
where o.CompeditorId.Equals(CompeditorId)
join o3 in _db.Event_Classes on o2.EventClassID equals o3.EventClassID
where o2.EventClassID.Equals(o3.EventClassID)
join o4 in _db.Class_Definitions on o3.ClassID equals o4.Class_Definition_ID
where o3.ClassID.Equals(o4.Class_Definition_ID)
select new InEventClass()
{
ClassName = o4.Class_Name,
EventClassID = o2.EventClassID,
IsSelected = true
}).ToList();
//pull a complete list of Event Classes that are avaiaible
var totallist = (from o in _db.Event_Classes
join o2 in _db.Event_Classes on o.ClassID equals o2.ClassID
where o.ClassID.Equals(o2.ClassID)
join o3 in _db.Class_Definitions on o2.ClassID equals o3.Class_Definition_ID
where o2.ClassID.Equals(o3.Class_Definition_ID)
join o4 in _db.Events on o.EventID equals o4.EventID
where o.EventID.Equals(o4.EventID)
where o4.CurrentEvent.Equals(true)
select new AllEventClasses()
{
ClassName = o3.Class_Name,
EventClassID = o2.EventClassID,
IsSelected = false
}).ToList();
var whatsleft = totallist.Where(eachtotalclass => !(nlist.Any(eachClassIHave => eachClassIHave.EventClassID == eachtotalclass.EventClassID))).ToList();
var model = new CompDetailPlus { AllEventClasses = whatsleft, Compeditor = comp, InEventClass = nlist };
return View(model);
}
View:
(Has to show the Competitor detail and a compound list of Event_Classes they are in)
In the view, I cannot see the values for any data.. all error on run and no good for display.
#model IEnumerable<eManager.Web2.Models.CompDetailPlus>
#{
ViewBag.Title = "Competitor's Detail";
}
<h2>#ViewBag.Title</h2>
<fieldset>
<legend>Compeditor</legend>
<table border="1" >
<tr>
<td>
<div class="display-field">
#Html.HiddenFor(model => model.Compeditor.CompeditorId)
</div>
<b>First Name</b>
<div class="display-field">
#Html.DisplayFor(model => model.Compeditor.FirstName)
</div>
</td>
<td>
<b>Last Name</b>
<div class="display-field">
#Html.DisplayFor(model => model.Compeditor.LastName)
</div>
</td>
#using (Html.BeginForm("CompeditorDetail", "Compeditor", FormMethod.Post))
{
foreach (var item in Model)
{
<input type="checkbox" name="MyID" value="#item.AllEventClasses.IsSelected"/> #item.InEventClass.ClassName <br />
<input type="hidden" name="CompeditorID" value="#item.InEventClass.CompeditorId" />
}
}
</td>
Your View accepts a model of IEnumerable eManager.Web2.Models.CompDetailPlus which would be fine, but your controller is sending a single eManager.Web2.Models.CompDetailPlus object.
Try changing this in your View
#model IEnumerable<eManager.Web2.Models.CompDetailPlus>
to this:
#model eManager.Web2.Models.CompDetailPlus
And change the bottom part of your view so that it's iterating through Enumerable compaosite items inside your model.
My task is to show multiple models into a single view.I've created a ViewModel for my requirement but I'm not meeting my requirement.
please have a look into the below code and rectify me where m i going wrong ???
public partial class StudentsDetail
{
public int StudentID { get; set; }
public int ParentID { get; set; }
public string StudentName { get; set; }
public string Gender { get; set; }
public string FatherName { get; set; }
public string MotherName { get; set; }
public Nullable<System.DateTime> DateOfBirth { get; set; }
public virtual ParentsDetail ParentsDetail { get; set; }
public virtual SchoolDetail SchoolDetail { get; set; }
}
//Model 2
public partial class ParentsDetail
{
public ParentsDetail()
{
this.StudentsDetails = new HashSet<StudentsDetail>();
}
public int ParentID { get; set; }
public string Occupation { get; set; }
public string Organization { get; set; }
public string AnnualIncome { get; set; }
public virtual ICollection<StudentsDetail> StudentsDetails { get; set; }
}
//ViewModel Which I have created
public class ParentsInformationViewModel
{
public List<StudentsDetail> StudentsDetails { get; set; }
public List<ParentsDetail> ParentsDetails { get; set; }
public ParentsInformationViewModel(List<StudentsDetail> _studentDetails, List<ParentsDetail> _parentsDetails) //Should i pass all the required parameters that i want to display in view ????
{
StudentsDetails = _studentDetails;
ParentsDetails = _parentsDetails;
}
//And finally this is my method defined in the StudentController (Have i defined it in a right place/way??)
public ActionResult StudentViewModel()
{
ViewBag.ParentsDetail = new ParentsDetail(); //ParentsDetail is my controller
List<StudentsDetail> studentListObj = StudentsDetailsDAL.GetStudentDetails();
List<ParentsInformationViewModel> ParentInfoVMObj = new List<ParentsInformationViewModel>();
//foreach (var student in studentListObj)
//{
// ParentInfoVMObj.Add(new ParentsInformationViewModel(student.StudentID, student.ParentID));
//}
//ParentInfoVMObj.Add(ParentInfoVMObj); /// don't know how to call the required viewmodel
return View(ParentInfoVMObj);
}
I know that the above method of a ViewModel is wrong but how to use it or where am i going wrong I can't get.
I want to display the ViewModel in the view as a detailed view .
Please, correct me as I'm a starter in MVC3 .
Thanks In Advance!!
In your controller, define your action method as follows.
public ActionResult ParentsDetails()
{
var studentDetails = new List<StudentDetail>();
var parentDetails = new List<ParentsDetail>();
// Fill your lists here, and pass them to viewmodel constructor.
var viewModel = new ParentsInformationViewModel(studentDetails, parentDetails)
// Return your viewmodel to corresponding view.
return View(viewModel);
}
In your view define your model.
#model MySolution.ViewModels.ParentsInformationViewModel
Is there in your view declared that you are receiving model of type
In view:
#model IEnumerable<ParentsInformationViewModel>