none
EF one to one relationship RRS feed

  • Question

  • I have two models of Product and productDetails, a product may or may be have a ProductDetails. How can I create the entities in the same view? When Editing or creating a product, I want it to be done in the same view. Here are the models below?
    // ProductDetails model
    
    public class ProductDetail
        {
            [Key]
            [ForeignKey("Product")]
            public int ProductDetailID { get; set; }
            public string Brand { get; set; }
            [Display(Name = "Operating System")]
            public string OperatingSystem { get; set; }
            [Display(Name = "Mother Board")]
            public string MotherBoard { get; set; }
            public string Processor { get; set; }
            public string Memory { get; set; }
            public string Storage { get; set; }
            public string Graphics { get; set; }
            public string Connectivity { get; set; }
            [Display(Name = "External Ports")]
            public string ExternalPorts { get; set; }
    
            public virtual Product Product { get; set; }
        }

    // Product Model
    public class Product
        {
            public int ID { get; set; }
    
            public int? CategoryID { get; set; }
    
            [Display(Name = "Product Name")]
            [Required(ErrorMessage = "The product name cannot be blank")]
            public string Name { get; set; }
    
            [Required(ErrorMessage = "The product description cannot be blank")]
            [StringLength(200, MinimumLength = 10, ErrorMessage = "Please enter a product description between 10 and 200 characters in length")]
            [DataType(DataType.MultilineText)]
            public string Description { get; set; }
    
    
            [Display(Name = "Price ")]
            [Required(ErrorMessage = "The price cannot be blank")]
            [DataType(DataType.Currency)]
            [DisplayFormat(DataFormatString = "{0:c}")]
            [RegularExpression("[0-9]+(\\.[0-9][0-9]?)?", ErrorMessage = "The price must be a number up to two decimal places")]
            public decimal Price { get; set; }
    
            [Display(Name = "Last Modified")]
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
            /* [HiddenInput(DisplayValue=false)]*/
            public DateTime ModifiedDate { get; set; }
    
    
            public virtual Category Category { get; set; }
            public virtual ICollection<ProductImage> ProductImages { get; set; }
        }

    • Moved by Fei Hu Monday, March 26, 2018 5:36 AM Moved From C#
    Sunday, March 25, 2018 5:45 PM

All replies

  • , I want it to be done in the same view. Here are the models below?

    Your question is really not about EF. Your question is about controlling the viewmodels, views and partial views.

    You need to figure that out first.

    You should post to the MVC forum in ASP.NET forums for help. 

    https://forums.asp.net/

    I'll give you this to think about.

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

    <copied>

    An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.+

    A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.+

    In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

    <end>

    Sunday, March 25, 2018 6:22 PM
  • Hello niftymic,

    Based on your description, I am afraid that the issue is out of support of C# forum which mainly discusses C# programming language, IDE, libraries, samples and tools.

    I suggest that you can consult your issue directly on asp.net forum for better solution and support. If I have misunderstood anything, please feel free to let me know.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 26, 2018 5:35 AM
  • Thanks, i think that's a good suggestion. I'm new in web development with ASP.Net, and most of the time I get confuse with the C#, asp.net, entity framework and all that staff. Hope to find my way out one day.

    Thanks again and sorry for any problem created.


    niftymic

    Friday, March 30, 2018 9:48 PM
  • Thanks for your advice and help. I will try them out.

    niftymic

    Friday, March 30, 2018 9:50 PM