net MVC 5 I have a view that require info from two models. In one modelView i have Student Marks and in the other, I have Student details How do I call both models to the view.
public class Student
{
[Key]
public int StudentNum { get; set; }
public string StudentName { get; set; }
public string StudentSurname { get; set; }
public string Email { get; set; }
}
public class Marks
{
[Key]
public int Mark1 { get; set; }
public int Mark2 { get; set; }
public int Dp { get; set; }
public int StudentNum { get; set; }
public virtual Student Student { get; set; }
}
Create a new View Model which encapsulates both the other view models as,
public class StudentViewModel
{
public Details Details { get; set; }
public Marks Marks { get; set; }
}
and pass it to the view you use. And access by Model.StudentViewModel.Details for getting student details and for marks Model.StudentViewModel.Marks.
Related
I have two model classes "customerdetails" and "addresssdetails".my question is "one customer can save with multiple addresss?
namespace customer2.Models
{
public class customerdetails
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { set; get; }
[Key]
public int customerid { set; get; }
public string customername { set; get; }
}
public class addressdetails
{
public int addressno { set; get; }
public string street { set; get; }
public string landmark { set; get; }
public int pincode { set; get; }
}
public class MkContext : DbContext
{
public virtual DbSet<customerdetails> customers { get; set; }
public virtual DbSet<addressdetails> address { get; set; }
}
public class customerviewmodel
{
public customerdetails cd { set; get; }
public List<addressdetails> ad { set; get; }
}
}![enter image description here](https://i.stack.imgur.com/4QCA1.jpg)
Before I answer, just a quick tip:
Make sure to properly case your class names and properties to improve readability.
Here is a link with some guidelines
If you want to store a Customer with multiple AddressDetails, you will need to define a class and add it to the context.
public class Customer {
public int Id { get; set; }
public CustomerDetails CustomerDetails { get; set; }
public IList<AddressDetails> AddressDetails { get; set; }
}
Then add this to the context.
I want to use multiple model in one view, I red some article to use complex Model, or Partial View, or Viewbag and other,but couldn't find normal answer.
I want to display data from different models.
Here is my Models
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public string Image { get; set; }
public IEnumerable<Teacher> Teachers { get; set; }
}
public class Teacher
{
public int TeacherID { get; set; }
public string TeacherName { get; set; }
public string TeacherLname { get; set; }
public int DepartmentID { get; set; }
public string Image { get; set; }
public Department Department { get; set; }
}
public class ClassOut
{
public int Id { get; set; }
public string ClassOutName { get; set; }
}
public class SchoolDbContext:DbContext
{
public DbSet<Teacher> Teachers { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<ClassOut> ClassOuts { get; set; }
}
Combine all your models in one common view model and use it in your view.
public class ViewModelName
{
public Department Department {get; set;}
public Teacher Teacher {get; set;}
public ClassOut ClassOut {get; set;}
}
Use this model for your view:
#model ViewModelName
......
#Html.TextBoxFor(x => x.Teacher.TeacherName )
.....
And in your post action method
public ActionResult MethodName(ViewModelName viewModel)
{
//
}
Try to create a Class ( for example Manager) where you add a variable for Department, ClassOut, ClassOutName and then you pass the Class Manager to the view and then you can access easily
public class Manager
{
public Department dept;
public ClassOut cl;
public ClassOutName clname;
}
I am trying to create a website that allows user to upload recipe
So I have a model class called Recipe and it looks like following
[Table("Recipe")]
public class Recipe
{
public int ImageMimeType { get; set; }
public byte[] Thumbnail { get; set; }
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RecipeID { get; set; }
public string BakeryName { get; set; }
public string Description { get; set; }
public byte[] ImageData { get; set; }
public int EndorsementCount { get; set; }
public DateTime LastModified { get; set; }
[Required]
public int UserId { get; set; }
public virtual ICollection<Ingredient> Ingredients { get; set; }
public virtual ICollection<Method> Methods { get; set; }
}
and my Ingredient class looks like follow:
[Table("Ingredient")]
public class Ingredient
{
[Key]
public int IngredientID { get; set; }
[Required]
public int RecipeID { get; set; }
public string ItemName { get; set; }
public int Measurement { get; set; }
public string MeasurementUnit { get; set; }
}
And in my View I want to add ingredient dynamically: User click an 'add' button to add another ingredient when she wants to.
My questions are:
How should I create a list in my View for the ingredients and pass it to my Controller's Create method when I submit my form?
And the problem is, as the RecipeID is Autogenerated, I don't know what my RecipeID is going to be while in the View until I save my Recipe in the database table. But how to I pass this RecipeID to each of my Ingredients?
Any help will be very much appreciated!
I want to create a form for the model below:
public class SPObject
{
public int ID { get; set; }
public string identifier { get; set; }
public Card Card { get; set; }
public ICollection<Product> Products { get; set; }
}
A simple and basic example would be highly appreciated.
Today I've been working with MVC for the first time. Also normally I use the EF with model first, but I wanted to try POCO.
So I've made my 3 entities and when I try to make a controller I get an error:
Unable to retrieve metadata for "BookExchange.Models.Exchange". Unable to determine the principal end of an association between the types "BookExchange.Models.Exchange" and "BookExchange.Models.Book". The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
My 3 classes:
public class Book
{
public int BookID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string ISBN10 { get; set; }
public int PersonID { get; set; }
public virtual Person Person { get; set; }
public virtual Exchange Exchange { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Exchange> Exchanges { get; set; }
}
public class Exchange
{
[Key]
public int BookID { get; set; }
public int PersonID { get; set; }
public DateTime ReturnDate { get; set; }
public virtual Person Person { get; set; }
public virtual Book Book { get; set; }
}
Does anyone know what I'm doing wrong? I don't want to lose the association properties.
Thanks in advance!
Try adding foreign key properties for your references. E.g.
public class Book
{
public int BookID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string ISBN10 { get; set; }
public virtual Person Person { get; set; }
public int PersonID { get; set; }
public virtual Exchange Exchange { get; set; }
public int ExchangeID { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Exchange> Exchanges { get; set; }
}
public class Exchange
{
public int ExchangeID { get; set; }
public int BookID { get; set; }
public virtual Book Book { get; set; }
public int PersonID { get; set; }
public virtual Person Person { get; set; }
public DateTime ReturnDate { get; set; }
}
Also, take a look at ScottGu's post on code first and this EF post on conventions.
Try this: (Remove database, so EF will create new)
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public virtual Person Person { get; set; }
public virtual Exchange Exchange { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Exchange> Exchanges { get; set; }
}
public class Exchange
{
public int Id { get; set; }
public DateTime ReturnDate { get; set; }
public virtual Person Person { get; set; }
public virtual Book Book { get; set; }
}
It's your one on one associations.
Remove one reference between exchange or book, so Code-first can decide which one is more important in your one on one relation (Book <--> Exchange)
If you want to know why, you should read this: