title: MVC 4 Inserting Model Data into a database using Repository Framework
body: <p>Here is my ListView Model which moreorless corresponds with a datbase table I have built called Comment. </p>
  
  <pre><code> public int EntryId { get; set; }
      public DateTime Posted { get; set; }
      public string AuthorIp { get; set; }
  
      [Required(ErrorMessage = " A Name is required *")]
      [DisplayFormat(ConvertEmptyStringToNull = false)]
      [StringLength(160, MinimumLength = 2, ErrorMessage = "Must be between 2 &amp; 160 characters in length.")]
      public string AuthorName { get; set; }
  
      [Required(ErrorMessage = "Email address required *")]
      [DisplayFormat(ConvertEmptyStringToNull = false)]
      [StringLength(160, MinimumLength = 2, ErrorMessage = "Must be between 2 &amp; 160 characters in length *")]
      [EmailValidation(ErrorMessage = "Must be valid email *")]
      public string AuthorEmail { get; set; }
  
      [Required(ErrorMessage = " A Message is required *")]
      [DisplayFormat(ConvertEmptyStringToNull = false)]
      [StringLength(4000, MinimumLength = 2, ErrorMessage = "Must be between 2 &amp; 4000 characters in length *")]
      public string Body { get; set; }
  
      public ListView(IBlogRepository blogRepository)
      {
          Posts = blogRepository.Posts();
      }
      public ListView(){ }`
  </code></pre>
  
  <p>I need to get some of the properties into my Comment table. Iam using or at least attempting to use the IRepository Framework. It goes like this...</p>
  
  <pre><code> public interface IBlogRepository : IDisposable
  {
      IList&lt;Entry&gt; Posts();
  
      void InsertComment(Comment comment);
      void Save();
  }
  </code></pre>
  
  <p>Here is my inherit class...</p>
  
  <pre><code>public class BlogRepository : IBlogRepository, IDisposable
  {
      private BlogDataDataContext _dataContext;
      public BlogRepository() { _dataContext = new BlogDataDataContext(); }
  
     // #region IBlogRepository Members
  
      public void InsertComment(Comment comment)
      {
          _dataContext.Comments.InsertOnSubmit(comment);
      }
  
      public void Save()
      {
          _dataContext.SubmitChanges();
      }
  </code></pre>
  
  <p>So I call above InsertComment from my BlogController like this.</p>
  
  <pre><code>       [HttpPost]
          public ActionResult BlogPost(ListView pModel)
          {
  
              pModel.Posted = DateTime.Now;
              _repository.InsertComment( // Cannot pass pModel as is not Comment type.
  
              return RedirectToAction("BlogPost");
          }
  </code></pre>
  
  <p>So my problem is that my ListView pModel is passed in but its not of type Comment so I cant Insert it properly. I need my ListView model because it contains extra validation rules and a couple of constructors. Anyone have an idea of where I am going wrong.</p>
  
  <p>Do I need to create a Model which directly mirrors the datbase table I am asdding too. Then where do I move my Constructors and other validation rules? It feels like I need two models. One on top of the other. I'm so close to understanding this now.</p>
  
  <p>Thanks in advance.</p>