1 Replies - 1894 Views - Last Post: 28 June 2016 - 07:28 AM

#1 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 349
  • Joined: 12-September 08

asp.net mvc strong typed view

Posted 30 March 2016 - 04:29 PM

Using asp.net mvc 4 code first

I have Post class and I have used strongly typed view to display posts.
I have table which is attachments and I need to store those attachment according to post.

Question 1 :
How I can display attachments from other table / class and also edit attachment using strongly typed view?

Question 2 :
How I can get last inserted ID from post table to store inside attachment table.



classes are below
POST Class
public class Posts
    {
        [Key]
        public int id { get; set; }
        public int author_id { get; set; }
        public string title { get; set; }

        public string content_summary { get; set; }
        public string content_procedure { get; set; }
        public int content_rates { get; set; }
        public string content_references { get; set; }

        public int status { get; set; }
        public DateTime? created { get; set; }
        public DateTime? updated { get; set; }

    }



attachment
  {
        [Key]
        public int id { get; set; }
        public int post_id { get; set; } // fk

        public string file_type { get; set; }
        public string path { get; set; }
        public string file_name { get; set; }
        public string comments { get; set; }
        public string title { get; set; }
        public DateTime? date_time_created { get; set; }
        public int status { get; set; }
        public string keywords { get; set; }

        
        
        
        
    }

This post has been edited by dreamincodehamza: 30 March 2016 - 09:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: asp.net mvc strong typed view

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: asp.net mvc strong typed view

Posted 28 June 2016 - 07:28 AM

You will need a ViewModel to handle what you are attempting to do. With a ViewModel, you can essentially group "like" entities in to a single entity to create a strongly-typed object for the view.

Yours would look something similar to the following
public class PostAttachmentVM
    {
        public int id { get; set; }
        public int author_id { get; set; }
        public string title { get; set; }
        public string content_summary { get; set; }
        public string content_procedure { get; set; }
        public int content_rates { get; set; }
        public string content_references { get; set; }
        public int status { get; set; }
        public DateTime? created { get; set; }
        public DateTime? updated { get; set; }
        public int id { get; set; }
        public int post_id { get; set; } // fk
        public string file_type { get; set; }
        public string path { get; set; }
        public string file_name { get; set; }
        public string comments { get; set; }
        public string title { get; set; }
        public DateTime? date_time_created { get; set; }
        public int status { get; set; }
        public string keywords { get; set; }
    }



You can of course, strip out the properties you're not using in the view. In the controller, you would need to pull both the Attachments and Posts entities to populate the View Model. Once the ViewModel is populated, you can do everything you would normally do with a model with it. To insert/update the entity, pull the original from the database, then use the ViewModel properties to assign them to the original, then perform the actual update.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1