1

I have mission system in MVC. and I give a same mission or diffrent mission for a lot of users. and I show title, description, start date , finish date and who are/is in mission. These are showing view page in grid.mvc. but when I login I can see every mission. I dont want to every mission I just want to see only my mission. of course other users see their missions.

in my controller, I split names.

this is my Codes,

Controller:

TicketDbContext db = new TicketDbContext();

    public ActionResult Index()
    {       


        var result = db.Missions.OrderByDescending(x=>x.GivenDate).ToList();

        string ad = string.Empty;
        foreach (var item in result)
        {
            if (!string.IsNullOrEmpty(item.GivenUsers))
            {
                string[] Ids = item.GivenUsers.Split(',');

                for (int i = 0; i < Ids.Length; i++)
                {
                    int id = Convert.ToInt32(Ids[i]);
                    item.GivenUsers += db.Users.FirstOrDefault(x => x.Id == id).Name+ " " + db.Users.FirstOrDefault(x => x.Id == id).Surname+ ",";
                } 
            }               

        }

        return View(result);

    }

ScreenShot of my Grid

enter image description here

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Oğuz Özdemir
  • 89
  • 3
  • 12

1 Answers1

1

firstly, I recommend that you keep the assigned users ids in separated table

This is can handle your case..

var currentUserId = "";// set current userId from Where are you holding (session,identity etc..)
var result = db.Missions.OrderByDescending(x=>x.GivenDate).ToList();
result = result.Where(x=> x.GivenUsers.Split(',').Contains(currentUserId));
    foreach (var item in result)
    {
        if (!string.IsNullOrEmpty(item.GivenUsers))
        {
            int[] Ids = Array.ConvertAll(item.GivenUsers.Split(','),
                                      delegate(string s) { return int.Parse(s); }) ;
            string[] names = db.Users.Where(u => Ids.Contains(u.Id)).Select(u => u.Name+ " " + u.Surname).ToArray();
            item.GivenUsers = string.Join(",",names);

        }               

    }
levent
  • 3,464
  • 1
  • 12
  • 22
  • I have error. var currentUserId = this.Session.SessionID;// set current userId from Where are you holding (session,identity etc..) var result = db.Gorevs.OrderByDescending(x => x.VerildigiTarih).ToList(); result = db.Gorevs.Where(x => x.GoreviAlanUye.Split(',').Contains(currentUserId)); // in this line. the program say Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) – Oğuz Özdemir Mar 28 '17 at 13:03
  • sorry, I Changed `result = db.Gorevs.Where(x => x.GoreviAlanUye.Split(',').Contains(currentUserId));` to `result = result .Where(x => x.GoreviAlanUye.Split(',').Contains(currentUserId));` on answer. – levent Mar 28 '17 at 13:41
  • same mistake. "Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast? " if I put end of the line "ToList()" program is working but my view page showing nothing. grid is empty – Oğuz Özdemir Mar 28 '17 at 14:01
  • `this.Session.SessionID` is not your current user Id. you need current authenticated userId – levent Mar 28 '17 at 14:04