I am very new to MVC and I am trying to make a CreateEmployee form in MVC. for now, all I am trying to achieve is to add the poulated dropdownlist for Departments to the form. the dropdownlist is populated from a database, and using Visual Studio, I connected to the DB and it created all the code file for the table. This is what the create form should look like. The form below is created using Angular js.
here is my Createform model.
public class CreateEmployee
{
public int Id { get; set; }
public string FullName { get; set; }
[Required]
public string Notes { get; set; }
//add the dropdown list of departments here,i not sure on what to do here
//do i create an instance of dbcontext here or AngtestDepartment
public bool PerkCar { get; set; }
public bool PerkStock { get; set; }
public bool PerkSixWeeks { get; set; }
public string PayrollType { get; set; }
}
public ActionResult CreateEmployee()
{
return View();
}
Here are the table codes that visual studio generated
Department Table
using System;
using System.Collections.Generic;
public partial class AngTestDepartment
{
public int id { get; set; }
public string Department { get; set; }
}
and the Department Table context
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class DepartmentDbContext : DbContext
{
public DepartmentDbContext()
: base("name=DepartmentDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AngTestDepartment> AngTestDepartments { get; set; }
public System.Data.Entity.DbSet<AngularForms2.Models.CreateEmployee> CreateEmployees { get; set; }
}
You should query the DB in the controller and supply that to the View, e.g. via the model:
Add the following to your model:
public int Department { get; set; }
public IEnumerable<AngTestDepartment> Departments { get; set; }
and in your Action:
public ActionResult CreateEmployee()
{
using (var db = new DepartmentDbContext())
{
var model = new CreateEmployee();
model.Departments = db.AngTestDepartments.ToList();
return View(model);
}
}
then inside your view you can do something like:
#Html.DropDownListFor(m => m.Department,
Model.Departments.Select(d => new SelectListItem()
{
Value = d.id.ToString(),
Text = d.Department
}))
Related
I have set up a SelectList to select an album when creating a track for the chinook database. Following Microsoft docs instruction, for some reason (that I can't see as I'm still trying to learn) it doesn't like what I am putting in my view on razor pages. The code for the walk through is below.
Entities:
public class Album
{
public int AlbumId { get; set; }
public string Title { get; set; }
public int ArtistId { get; set; }
//related entities
public virtual ICollection<Track> Tracks { get; set; }
public virtual Artist Artist { get; set; }
}
public class Track
{
public int TrackId { get; set; }
public string Name { get; set; }
public int AlbumId { get; set; }
public string Composer { get; set; }
public int Milliseconds { get; set; }
public int Bytes { get; set; }
public double UnitPrice { get; set; }
public int MediaTypeId { get; set; }
public int GenreId { get; set; }
//related entities
public virtual Album Album { get; set; }
}
Creating the model for SelectedList:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using UWS.Project;
namespace Project.Pages.Albums
{
public class AlbumPageModel : PageModel
{
public SelectList AlbumNameSL { get; set; }
public void PopulateAlbumsDropDownList(Chinook db,
object selectedAlbum = null)
{
var albumsQuery = from a in db.Albums
orderby a.Title // Sort by name.
select a;
AlbumNameSL = new SelectList(albumsQuery,
"AlbumId", "Title", selectedAlbum);
}
}
}
Now my Model for creating the track:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using UWS.Project;
namespace Project.Pages.Albums
{
public class TrackCreateModel : AlbumPageModel
{
private Chinook db;
public TrackCreateModel(Chinook injectedContext)
{
db = injectedContext;
}
public IActionResult OnGet(int? id)
{
PopulateAlbumsDropDownList(db);
return Page();
}
[BindProperty]
public Track Track { get; set; }
public async Task<IActionResult> OnPostAsync()
{
var emptyTrack = new Track();
if (await TryUpdateModelAsync<Track>(
emptyTrack,
"track", // Prefix for form value.
s => s.AlbumId, s => s.Name, s => s.Composer, s => s.Milliseconds, s => s.Bytes, s
=> s.MediaTypeId, s => s.GenreId, s => s.UnitPrice))
{
db.Tracks.Add(emptyTrack);
await db.SaveChangesAsync();
return RedirectToPage("./Index");
}
// Select AlbumId if TryUpdateModelAsync fails.
PopulateAlbumsDropDownList(db, emptyTrack.AlbumId);
return Page();
}
}
}
Trying to implement the SelectList in my view:
Being new to this I cannot for the life of me see the issue. I have looked at previous issues on here with SelectedList's but none relate to my issue. Any help would be appreciated.
Do not enter selectedValue in the PopulateAlbumsDropDownList method in AlbumPageModel
Currently you either leave it blank or provide an id, but that's not going to work anyway
I'm using a database first approach and I want to insert some data into the database. I am using this code:
public ActionResult Create(StudentDetails studentDetails)
{
using (StudentRecordManagementEntities1 obj = new StudentRecordManagementEntities1())
{
obj.StudentDetails.Add(studentDetails); //throws error
}
}
Model class:
public class StudentDetails
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public Nullable<long> ContactNumber { get; set; }
public string Address { get; set; }
public decimal Fees { get; set; }
public bool isPaid { get; set; }
}
Error details:
Error CS1503 Argument 1: cannot convert from 'StudentRecordManagement.Models.StudentDetails' to 'StudentRecordManagement.Models.StudentDetail' StudentRecordManagement*
If you need more details, kindly let me know.
It looks like your model class is StudentRecordManagement.Models.StudentDetail, however you are inserting StudentRecordManagement.Models.StudentDetails. So what you can do is to give necessary type to your Database model:
public ActionResult Create(StudentDetails studentDetails)
{
using (StudentRecordManagementEntities1 obj = new StudentRecordManagementEntities1())
{
var studentDetail = new StudentRecordManagement.Models.StudentDetail {
Name = studentDetails.Name,
// the other code is omitted for the brevity
};
obj.StudentDetails.Add(studentDetail); //
}
I have a probably simple question, I am trying to create many to many relationships using entity framework and fluent api, and my problem is that when i try to do any query or view a object in debug it has always 0 items.
I am using junction table that looks like:
So relations exists, to be sure ive checked:
select candidate.firstname, skillset.name
from candidate
join candidate_skillset on candidate.id = candidate_skillset.candidate_id
join skillset on candidate_skillset.skillset_id = skillset.id
and joined results are displayed.
Now my context looks like:
public class CatalogContexct : DbContext
{
public DbSet<Candidate> Candidates { get; set; }
public DbSet<SkillSet> SkillSets { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Candidate>().HasMany(t => t.SkillSets).WithMany(t => t.Candidates)
.Map(m =>
{
m.ToTable("candidate_skillset");
m.MapLeftKey("candidate_id");
m.MapRightKey("skillset_id");
});
modelBuilder.Entity<SkillSet>().ToTable("skillset");
modelBuilder.Entity<Candidate>().ToTable("candidate");
}
}
My left side model candidates:
[Table("candidate")]
public class Candidate
{
public Candidate()
{
this.SkillSets = new HashSet<SkillSet>();
}
[Key]
public int id { get; set; }
[Column("firstname")]
public string Firstname { get; set; }
public int? commendation_id { get; set; }
[ForeignKey("commendation_id")]
public Commendation commendation { get; set; }
public ICollection<SkillSet> SkillSets { get; set; }
}
And my rightside model skillset:
[Table("skillset")]
public class SkillSet : SimpleDictionary
{
public SkillSet()
{
this.Candidates = new HashSet<Candidate>();
}
public virtual ICollection<Candidate> Candidates { get; set; }
}
and that model has a parent class:
public class SimpleDictionary
{
[Key]
public int id { get; set; }
[Column("name")]
public string Name { get; set; }
}
So all should work but when I do for example:
var ca = this._catalog.Candidates
.Include("SkillSets").Include("commendation").
FirstOrDefault(x => x.SkillSets.Any());
Result is null, also when I view object on debug collection of property skillset allays has 0 elements, any idea what could be wrong with it?
I tried this with same structure mentioned here in you question and tried locally . And I am able to get the data with this code . Please try this and let me know if this helps . I just omitted commendation table for simplicity .
var context = new SampleDbContext();
var candidates = context.Candidates
.Include("SkillSets").ToList();
foreach (var candidate in candidates)
{
foreach (var sk in candidate.SkillSets.Where( s1 => s1.Candidates.Count(c=>c.id == candidate.id)>0 ))
{
Console.WriteLine( string.Format(#" Name : {0} Skill :{1}",candidate.Firstname ,sk.Name ) );
}
}
Below is my DbContext and Other Entity Classes
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class SampleDbContext : DbContext
{
public SampleDbContext()
: base("name=SampleDBConnection")
{
this.Configuration.LazyLoadingEnabled = false;
}
public DbSet<Candidate> Candidates { get; set; }
public DbSet<SkillSet> SkillSets { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Candidate>().HasMany(t => t.SkillSets).WithMany(t => t.Candidates)
.Map(m =>
{
m.ToTable("candidate_skillset");
m.MapLeftKey("candidate_id");
m.MapRightKey("skillset_id");
});
modelBuilder.Entity<SkillSet>().ToTable("skillset");
modelBuilder.Entity<Candidate>().ToTable("candidate");
}
}
[Table("candidate")]
public class Candidate
{
public Candidate()
{
this.SkillSets = new HashSet<SkillSet>();
}
[Key]
public int id { get; set; }
[Column("firstname")]
public string Firstname { get; set; }
public int? commendation_id { get; set; }
//[ForeignKey("commendation_id")]
//public Commendation commendation { get; set; }
public ICollection<SkillSet> SkillSets { get; set; }
}
public class SimpleDictionary
{
[Key]
public int id { get; set; }
[Column("name")]
public string Name { get; set; }
}
[Table("skillset")]
public class SkillSet : SimpleDictionary
{
public SkillSet()
{
this.Candidates = new HashSet<Candidate>();
}
public virtual ICollection<Candidate> Candidates { get; set; }
}
}
The output of the query you mentioned and the result of my code both matched I hope this is that you wanted .
I have a annoying problem with my code.
My model :
public class Option
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Conference> Conference { set; get; }
}
public partial class Conference
{
[Key, ForeignKey("User")]
public int UserId { get; set; }
public virtual Option Option { set; get; }
public virtual User User { get; set; }
}
public partial class User
{
public int Id {get; set; }
public string Name { get; set; }
public virtual Conference Conference { get; set; }
}
And now i`m getting Option object from Db by dbSet.Find(id) (RepositoryFactory) and what i want to do is to save newly created User, but with selected Option.
If i do like that:
var option = dbSet.Find(id);
var user = new User()
{
Name = "Name",
Conference = new Conference
{
Option = option
}
};
//...
context.SaveChanges();
I`m getting an exception:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
What I`m doing wrong?
Edit: I Tried to create Mapping, but this doesn`t seems to work too.
modelBuilder.Entity<Conference>()
.HasKey(x => x.UserId)
.HasRequired(x => x.User)
.WithOptional(user => user.Conference);
modelBuilder.Entity<Option>()
.HasMany(option => option.Conferences)
.WithRequired(conference => conference.Option)
.HasForeignKey(conference => conference.UserId);
Are you trying to achieve a 1:1 relationship between User and Conference? If so, you need to add an Id (key) property to User. Please see the comments I added to the code sample below regarding the 1:1 relationship. I would advise further evaluation of your domain layer to ensure this is what you are trying to achieve...
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace Stackoverflow
{
public class EntityContext : DbContext
{
public IDbSet<Conference> Conferences { get; set; }
public IDbSet<Option> Options { get; set; }
public IDbSet<User> Users { get; set; }
}
public class Option
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Conference> Conference { set; get; }
}
public class Conference
{
// In one-to-one relation one end must be principal and second end must be dependent.
// User is the one which will be inserted first and which can exist without the dependent one.
// Conference end is the one which must be inserted after the principal because it has foreign key to the principal.
[Key, ForeignKey("User")]
public int UserId { get; set; }
public int OptionId { get; set; }
public virtual Option Option { set; get; }
public virtual User User { get; set; }
}
public class User
{
// user requires a key
public int Id { get; set; }
public string Name { get; set; }
public virtual Conference Conference { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var entityContext = new EntityContext())
{
// added to facilitate your example
var newOption = new Option {Name = "SomeOptionName"};
entityContext.Options.Add(newOption);
entityContext.SaveChanges();
var option = entityContext.Options.Find(newOption.Id);
var user = new User
{
Name = "Name",
Conference = new Conference
{
Option = option
}
};
// need to add the user
entityContext.Users.Add(user);
//...
entityContext.SaveChanges();
}
}
}
}
I am following this MVC tutorial and trying to create a database using DbContext and related model classes. The project name is "odeToFood".
Model classes:
namespace odeToFood.Models
{
public class Restaurant
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
public ICollection<RestaurantReview> Reviews { get; set; }
}
public class RestaurantReview
{
public int Id { get; set; }
public string Body { get; set; }
public int Rating { get; set; }
public int RestaurantId { get; set; }
}
public class odeToFoodDb :DbContext
{
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<RestaurantReview> Reviews { get; set; }
}
}
HomeController:
public class HomeController : Controller
{
odeToFoodDb _db = new odeToFoodDb();
public ActionResult Index()
{
var model= _db.Restaurants.ToList();
return View(model);
}
}
Index View
#model IEnumerable<odeToFood.Models.Restaurant>
#{
ViewBag.Title = "Home Page";
}
#foreach (var item in Model)
{
<div>
<h4>#item.Name;</h4>
Restaurant is in : #item.City #item.Country
<hr />
</div>
}
When I run this code, according to this tutorial it should create a database and the values should be fetched (when I enter in table) but in server explorer I cannot find a database.
Neither the Index View gives an error nor can I find a database in server explorer. I tried (Localdb)\v11.0 by going to "add connection" but still it does not show any database.
I would be grateful to know what's wrong.