简体   繁体   中英

MVC 4 Inserting Model Data into a database using Repository Framework

Here is my ListView Model which moreorless corresponds with a datbase table I have built called Comment.

 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 & 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 & 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 & 4000 characters in length *")]
    public string Body { get; set; }

    public ListView(IBlogRepository blogRepository)
    {
        Posts = blogRepository.Posts();
    }
    public ListView(){ }`

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...

 public interface IBlogRepository : IDisposable
{
    IList<Entry> Posts();

    void InsertComment(Comment comment);
    void Save();
}

Here is my inherit class...

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();
    }

So I call above InsertComment from my BlogController like this.

       [HttpPost]
        public ActionResult BlogPost(ListView pModel)
        {

            pModel.Posted = DateTime.Now;
            _repository.InsertComment( // Cannot pass pModel as is not Comment type.

            return RedirectToAction("BlogPost");
        }

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.

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.

Thanks in advance.

I think your approach looks good overall, but you'll need some way of obtaining that current Comment object to pass into your repository. Whether you create another object to house both objects, or refactor your code, I would recommend looking at the following article:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

If you use ASP.NET MVC your model should be an exact "copy" of your table. If you want some other informations to pass to your view you can use viewmodels.

In your example why don't you do something like:

 [HttpPost]
    public ActionResult BlogPost(ListView pModel)
    {

        pModel.Posted = DateTime.Now;

        foreach (Comment comment in ListView.Posts)
         {
            _repository.InsertComment(comment);
         }

        return RedirectToAction("BlogPost");
    }

You could use AutoMapper to map properties from your ViewModel to your Comments business model in your controller:

var comment = Mapper.Map<ListView, Comment>(pModel);

_repository.InsertComment(comment);

This is common practice when working with ViewModels or DTO's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM