DropDown box in ASP .Net MVC 3 Razor Entity Framework - asp.net

i am new to ASP .NET and Particularly in ASP .Net MVC3 Razor...
i have created view for Clients and Executives in MVC3 Razor.
What i did is 1st i created Model called Clients.cs
namespace MVCInetClient.Model
{
[Table("tbl_Customer")]
public class Clients
{
[Required,Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "Client ID")]
public int ClientId { get; set; }
[Required]
[Display(Name = "Client Name")]
public string ClientName { get; set; }
[Required]
[Display(Name = "Executive Name")]
public string ExecutiveName { get; set; }
[Required]
[Display(Name = "Contact Name")]
public string ContactPerson { get; set; }
[Display(Name = "Address")]
public string Add1 { get; set; }
[Display(Name = " ")]
public string Add2 { get; set; }
[Display(Name = " ")]
public string Add3 { get; set; }
[Display(Name = "Pincode")]
public string Pin { get; set; }
[Display(Name = "State")]
public string State { get; set; }
[Display(Name = "Country")]
public string Country { get; set; }
[Display(Name = "Phone")]
public string Phone { get; set; }
[Required]
[StringLength(10)]
[RegularExpression("\\d+")]
[Display(Name = "Mobile")]
public string Mobile { get; set; }
[Display(Name = "Fax")]
public string Fax { get; set; }
[Display(Name = "Email")]
public string Email { get; set; }
[Display(Name = "Website")]
public string Web { get; set; }
}
public class ClientsDbContext : DbContext
{
public DbSet<Clients> Clients { get; set; }
public DbSet<Executives> Executives{ get; set; }
}
}
After that i Created the Controller called ClientsController with Scaffolding Options,
Template : Controller With Read/Write actions and Views, using Entity Framework
Model Class : Clients (MVCInetClient.Models)
Data Context Class : ClientsDbContext (MVCInetClient.Models)
It Created View Create, Edit, Index, Delete Automatically and its working Fine too.
Similarly i did for model called Executives.cs
namespace MVCInetClient.Models
{
[Table("tbl_Executive")]
public class Executives
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "Executive ID")]
public int ExecutiveId { get; set; }
[Required]
[Display(Name = "Executive Name")]
public string ExecutiveName { get; set; }
[Display(Name = "Address")]
public string Add1 { get; set; }
[Display(Name = " ")]
public string Add2 { get; set; }
[Display(Name = " ")]
public string Add3 { get; set; }
[Display(Name = "Pincode")]
public string Pin { get; set; }
[Display(Name = "State")]
public string State { get; set; }
[Display(Name = "Country")]
public string Country { get; set; }
[Display(Name = "Phone")]
public string Phone { get; set; }
[Required]
[StringLength(10)]
[RegularExpression("\\d+")]
[Display(Name = "Mobile")]
public string Mobile { get; set; }
[Display(Name = "Email")]
public string Email { get; set; }
}
public class ExecutivesDbContext : DbContext
{
public DbSet<Executives> Executives { get; set; }
}
}
and this too Working Fine in all views(create, edit, delete)
What i need is, i need a Dropdown list of Executive name in Clients View instead of editor Field.
i looked some tutorials but i am confused...
Please help me to solve it...

Seems like you are using EF Code First with MVC.
The simplest answer is that you need to a List of Executives, pass the list in a model back to your View, create a select list, and render a drop down list.
public ActionResult Execx()
{
Model Model = new Model();
Model.execList = dbcontext.Executives;
return View(Model);
}
In your View:
#Html.DropDownListFor(model => model.execId, new SelectList(Model.execList, "ExecutiveId", "ExecutiveName "))
This is untested code. I slapped the pieces together to give you the idea.

Related

How to check completed orders when user is logged in?

Currently I create a shop and I use ASP Identity razor pages to login, logout and registration. I have default IdentityUser. I have also ASPNetUsers table and I want reference row UserId to my other table Orders. My main purpose to achieve is when user logg in, he can check his completed orders from database. I know how to use LINQ to get order from database, but I didn't know how to connect that with Identity. I also use Session for adding item to cart if it is important.
public class AppDbContext : IdentityDbContext<IdentityUser>
{
public AppDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Game> Games { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<SubGenre> SubGenres { get; set; }
public DbSet<CartItem> CartItems { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<ShipAddress> ShipAddresses { get; set; }
}
public class ShipAddress
{
[BindNever]
public int ShipAddressId { get; set; }
public List<Order> Orders { get; set; }
[Required(ErrorMessage = "Wpisz swoje imię!")]
[StringLength(50)]
[Display(Name = "Imię:")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Wpisz swoje nazwisko!")]
[StringLength(50)]
[Display(Name = "Nazwisko:")]
public string LastName { get; set; }
[Required(ErrorMessage = "Wpisz nazwę ulicy!")]
[StringLength(50)]
[Display(Name = "Ulica:")]
public string Address1 { get; set; }
[Required(ErrorMessage = "Wpisz numer domu/lokalu!")]
[StringLength(50)]
[Display(Name = "Nr domu/lokalu:")]
public string Address2 { get; set; }
[Required(ErrorMessage = "Wpisz kod pocztowy!")]
[StringLength(6)]
[Display(Name = "Kod pocztowy:")]
public string ZipCode { get; set; }
[Required(ErrorMessage = "Wpisz miejscowość!")]
[StringLength(50)]
[Display(Name = "Miejscowość:")]
public string City { get; set; }
[Required(ErrorMessage = "Wpisz numer kontaktowy!")]
[StringLength(9)]
[Display(Name = "Nr telefonu:")]
public string PhoneNumber { get; set; }
[BindNever]
public decimal OrderTotal { get; set; }
[BindNever]
public DateTime OrderPlaced { get; set; }
public IdentityUser User { get; set; }
public int IdentityUserId { get; set; }
}
Current Login User
var userID = User.Identity.GetUserId();
This will give your current login user id then you can apply linq for based on this id with and condition of completed order you store in table
Use User's Identity which u logged in by using
string userID = User.Identity.GetUserId();

The entity type 'Customer' cannot be mapped to a table because it is derived from 'ApplicationUser'

I am trying to use separate identity classes that inherit from ApplicationUser, and create a relationship between them. Business and Customer where a Business can have many Customers. I get an error when adding relationships. What is the proper way to achieve adding identity between classes with relationships?
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public virtual Business Business { get; set; }
public virtual Customer Customer { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
}
Business.cs
public class Business : ApplicationUser
{
public string BusinessUserId { get; set; }
[Required]
public string BusinessName { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
public ICollection<Customer> Customers { get; set; }
....
}
Customer.cs
public class Customer : ApplicationUser
{
public string CustomerUserId { get; set; }
public override Business Business { get; set; }
}
You are using TPH which will go into the same table(AspNetUser) for your above entities.
To use TPT, you could configure models like
public class ApplicationUser : IdentityUser
{
public string BusinessId { get; set; }
[ForeignKey("BusinessId")]
public virtual Business Business { get; set; }
public string CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual Customer Customer { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
}
public class Business
{
[Key]
public string BusinessUserId { get; set; }
[Required]
public string BusinessName { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
public virtual ApplicationUser User { get; set; }
public ICollection<Customer> Customers { get; set; }
}
public class Customer
{
[Key]
public string CustomerUserId { get; set; }
public string BusinessId { get; set; }
[ForeignKey("BusinessId")]
public Business Business { get; set; }
public virtual ApplicationUser User { get; set; }
}
DbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Business> Businesses { get; set; }
}

Multi-Level One to Many in ASP MVC5

I am working on a small project, where I have multi-level one-to-many model. But for some reason, the EF doesn't seem to understand this relationship and I receive an error when I create the MVC controller with EF.
Here are my model classes:
public class BusinessDomain
{
[Key]
public int BusinessDomainID { get; set; }
[Display(Name = "Business Domain Name")]
public string DomainName { get; set; }
[Display(Name = "Domain Manager Name")]
public string DomainManager { get; set; }
public virtual ICollection<BusinessProcess> BusinessProcesses { get; set; }
}
public class BusinessProcess
{
public int BusinessProcessID { get; set; }
[Display(Name = "Business Processs Name")]
public string ProcessName { get; set; }
[Display(Name = "Business Process Owner Name")]
public string ProcessOwner { get; set; }
public virtual ICollection<BusinessSubProcess> BusinessSubProcesses { get; set; }
public int BusinessDomainID { get; set; }
public virtual BusinessDomain BusinessDomain { get; set; }
}
public class BusinessSubProcess
{
public int BusinessSubProcessId { get; set; }
public string SubProcessName { get; set; }
public string SubProcessOwnerName { get; set; }
public int BusinessProcessID { get; set; }
public virtual BusinessProcess BusinessProcess { get; set; }
}
When I create a EF controller, I get following error: Dependent role refers to the key properties, the upper bound of the multiplicity of the Dependent role must be 1.
If I keep only two models (BusinessLine and BusinessProcess) it seems to work. I begin to wonder if ASP MVC doesnt support multilevel of one to many models?
Please tell me what am I doing wrong?
Thanks in advance!
The only difference that I see is that you do not include a reference to the BusinessLine object in the BusinessProcess class. You should include that and also mark it as a foreign key.
Also, you marked the BusinessSubprocessID as a foreign key to the BusinessProcess, which I believe is a mistake since you also hold a BusinessProcessID in your class.
As a side note, do you need the BusinessLineID in your subprocess class, since you can easily access it through its parent?
public class BusinessLine
{
[Key]
public int BusinessLineID { get; set; }
[Display(Name = "Business Domain Name")]
public string DomainName { get; set; }
[Display(Name = "Domain Manager Name")]
public string DomainManager { get; set; }
public virtual ICollection<BusinessProcess> BusinessProcesses { get; set; }
}
public class BusinessProcess
{
[Key]
public int BusinessProcessID { get; set; }
[Display(Name = "Business Processs Name")]
public string ProcessName { get; set; }
[Display(Name = "Business Process Owner Name")]
public string ProcessOwner { get; set; }
public virtual ICollection<BusinessSubProcess> BusinessSubProcesses { get; set; }
public int BusinessLineID { get; set; }
[ForeignKey("BusinessLineID")]
public virtual BusinessLine BusinessLine { get; set; }
}
public class BusinessSubProcess
{
[Key]
public int BusinessSubProcessId { get; set; }
public string SubProcessName { get; set; }
public string SubProcessOwnerName { get; set; }
public DateTime Created { get; set; }
public DateTime LastChange { get; set; }
public int BusinessProcessID { get; set; }
[ForeignKey("BusinessProcessID")]
public virtual BusinessProcess BusinessProcess { get; set; }
}

Why default asp Scaffolding skips some of the model properties while generating 'Create' and 'Edit' views?

I am trying to generate a scaffold controller in my project, which is in VS2015 community edition and based on entity framework 6.1.3. Every time I try to do so, it only generates portion of the model. I mean the properties below.
public string Number { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
model class
public class Students
{
[Key]
[ScaffoldColumn(false)]
public int StudentsID { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Student ID")]
public string Number { get; set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Firstname")]
public string Firstname { get; set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Surname")]
public string Surname { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Date Of Birth")]
public string DateOfBirth { get; internal set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Year")]
public int Year { get; internal set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Class")]
public int Class { get; internal set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Address")]
public string Address { get; internal set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Postal code")]
public string Postalcode { get; internal set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Doctor Name")]
public string DoctorName { get; internal set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Doctor Address")]
public string DoctorAddress { get; internal set; }
//[Required]
[DataType(DataType.Text)]
[Display(Name = "Doctor Postalcode")]
public string DoctorPostalcode { get; internal set; }
[DataType(DataType.Text)]
[Display(Name = "Meds Info")]
public string MedsInfo { get; internal set; }
}
Data context
public class SchoolContext : DbContext
{
public SchoolContext()
: base("DefaultConnection")
{
}
public DbSet<Students> students { get; set; }
}
I don't know what to do. Same problem occurs, no matter if it is in a new project. My scaffolding controller creation process is below:

How to retrive list of posts when searching in tags table [ASP MVC]

I want to make a search for post via tags , for example when I write in the search box "ASP" I will search for post they have same tags and return them as a list of post ...
Here is my tags table :
Also here is my book class :
[Table("Book")]
public partial class Book
{
[Key]
public int Book_id { get; set; }
[Required]
[Display(Name ="User Name")]
[StringLength(128)]
public string User_ID { get; set; }
[Required]
[Display(Name = "Title Name")]
[StringLength(70,MinimumLength =3)]
public string Book_name { get; set; }
[Display(Name = "Edition")]
[Range(1, 20)]
public int Edition { get; set; }
[Display(Name = "Category Name")]
public int Category_id { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[Display(Name = "Publish Date")]
public DateTime Publish_date { get; set; }
[StringLength(50)]
[Display(Name = "Author Name")]
public string Author_name { get; set; }
[StringLength(128)]
[Display(Name = "Cover Image")]
public string Book_Image { get; set; }
[StringLength(128,MinimumLength =8)]
[Url]
[Display(Name = "Download Link")]
public string Download_Link { get; set; }
[StringLength(250)]
[Display(Name = "Upload Pdf")]
public string pdf_file { get; set; }
[AllowHtml]
[Column(TypeName = "text")]
[Display(Name = "Description")]
public string Book_Description { get; set; }
public int View_Count { set; get; }
[StringLength(250,MinimumLength =2,ErrorMessage ="Min Tags Input lenght is 2 char")]
[Display(Name = "Tags")]
public virtual string TagsListing { get; set; }
public virtual IList<Tag> Tags
{ get; set; }
public virtual AspNetUser AspNetUser { get; set; }
public virtual Category Category { get; set; }
}
Also tag class :
public class Tag
{
public virtual int Id
{ get; set; }
public virtual string Name
{ get; set; }
public virtual string UrlSlug
{ get; set; }
public virtual string Description
{ get; set; }
public virtual IList<Book> Books
{ get; set; }
}
I just simply want to see if the search term that the visitor searched for can be founded in tags table , and return list of books related to the same tag..
thanks
Using LINQ
var searchTerm = "Swift";
var postList = db.Books.Where(s => s.Tags.Any(g => g.Name.Contains(searchTerm))).ToList();
Contains will do a SQL equivalent of LIKE '%Swift%' . You can also try StartsWith or EndsWith as needed.

Resources