0

Do you know how i could use the loop "while" and "foreach" to display the results For my code :

Control.cs :

IEnumerable<Acces> all_id = acces_Repository.FindAll("where email = '" + id_ + "'");
ViewBag.Acceslist = all_id;
ViewBag.Liste = listeRepository.FindAll(" order by id ASC ");

Function.cs :

public IEnumerable<Acces> FindAll(string where = "")
        {
            try { 
            using (IDbConnection dbConnection = Connection)
            {
                dbConnection.Open();
                return dbConnection.Query<Acces>("SELECT * FROM access "+where);
                }
            }
            catch (Exception ex)
            {
               ex.ToString();
                return null;
            }
        }

I would like for its result in acces.cshtml:

<select class=" chosen-select form-control form-control-sm" name="id" value="" multiple="multiple">
 @foreach (var item in ViewBag.Liste)
 {
    @foreach (var item12 in ViewBag.Acces_liste)
    {
       @if (item.Id == item12.Id)
       {
          <option value="@item12.Id" checked selected="selected">@item12.Id</option>
       } else {
          <option value="@item.Id">@item.Id</option>
       }
     }
 }
 </select>

I have two functions which do not return the same results but come from the same database. In order to display all the results but selecting the correct ones. To get this result: Result

2

3 Answers 3

0

Okay, let's try to go over on how you can display your result set on your View.

You can either use ViewBag to assign your list and then iterate over it on your View

OR

You can use a strongly typed model binding on your View. I prefer to use this method as it gives your a more concrete implementation.

Regarding your case, you can do this:

IEnumerable<Acces> all_id = acces_Repository.FindAll("where email = '" + id_ + "'");
ViewBag.Acceslist = all_id;

And you can simply loop over this list on your View like this:

<ul>
    @if(ViewBag.Acceslist !=null)
    {
      @foreach(var data ViewBag.Acceslist)
      {
        <li>@data.Id</li>        
      }
    }
</ul>

You would need to check for null otherwise your View would crash if a null value is encountered.

You can use LINQ methods to do a join our lists and get your required data in one single list which you can send to your View. You can do something like:

IEnumerable<Acces> all_id = acces_Repository.FindAll("where email = '" + id_ + "'");
var liste = listeRepository.FindAll(" order by id ASC ");

var all_id_list=all_id.ToList();
var liste_list=liste.ToList();

var myList= from a in all_id_list
           join l in liste_list on a.Id equals l.Id
           where a.EmailId == "[email protected]"

ViewBag.displayMyList=myList;

And then you can loop over this ViewBag on your View to show the result.

Sign up to request clarification or add additional context in comments.

11 Comments

Yes thanks work with your code, but I take this opportunity to go further. I modified, for my final result
I've used this before but I haven't been able to compare the two results
I would like to compare two results (like acces.cshtml) and be able to select the ones that need to be
I edited, I don't know if you see i research
yes select or deselect and to have rights on one of the selected choices
|
0

You can simply do this:

 int temp = 0;

 foreach (Acces item in all_id)
 {
    ViewBag.liste[temp] = item.Id;
    temp ++;
 }

Or you can simply do this:

for (int temp = 0; temp < all_id.Count(); temp++)
{
  ViewBag.liste[temp] = all_id[temp].Id;
}

Comments

-1

you can Use Why Not but i think you if Use this code then it is better

for  (int i=0;i < all_id.Count();i++)
{
     foreach (Acces item in all_id)
     {
        ViewBag.liste[i] = item.Id;
     }

}

7 Comments

Thanks but i have this error : 'Cannot perform runtime binding on a null reference'
you must send full code can we found where is your problem
Every item in Viewbag will have the last item from all_id...
I have edit my post. I would like to recover all the results :)
@Toomy please make sure you check your Viewbag as that seems to be null
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.