K
K
Ko12016-09-17 15:10:27
ASP.NET
Ko1, 2016-09-17 15:10:27

Asp.net where to put the code in the controller?

Good day. There is a controller method, operations with the database are performed in it, etc. How correct is it to write code of this kind in the controller method. The project can grow and there will be more and more code. Does it need to be taken somewhere? Or is this normal practice?

public ActionResult Index()
        {
            var assessmentList = Session.GetAssessments();
            MySqlContext ctx = new MySqlContext();
            var dbPosts = ctx.Posts.Take(PostsPerPage).ToList();
            List<PostDTO> posts = null;

            if (assessmentList != null && assessmentList.Count > 0)
            {
                posts = dbPosts
                    .GroupJoin(assessmentList, o => o.Id, i => i.Id, (o, i) =>
                        new { dbPosts = o, SessPosts = i })
                    .SelectMany(
                        oi => oi.SessPosts.DefaultIfEmpty(),
                        (o, i) => new { dbPosts = o.dbPosts, SessPosts = i }
                    )
                    .Select(p => new PostDTO() {
                        Id = p.dbPosts.Id,
                        Content = p.dbPosts.Content,
                        Like = p.SessPosts == null ? null : p.SessPosts.Like
                    })
                    .ToList();
            }
            else
            {
                posts = dbPosts
                    .Select(x => new PostDTO() {
                        Id = x.Id,
                        Content = x.Content,
                        Like = null
                    })
                    .ToList();
            }

            return View(posts);
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tsiren Naimanov, 2016-09-17
@Ko1

https://chsakell.com/2015/02/15/asp-net-mvc-soluti...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question