-1

ok so this is maybe very simple but I just can not see where I am going wrong so any help is grateful

I have a model of this:

public IEnumerable<SelectedProfiels> profiles { get; set; }
public class SelectedProfiels
{
    public int Identifer { get; set; }
    public string ProfileName { get; set; }
}

and then I am trying to read from this

i am then building an array called selectedProfiles,

I then have this code

 var pro = from s in selectedProfiles select s;

What im wanting to do is find all the ProfileName's and assigned them there on ViewBag.[i] or just there own string variable

I have a model collection like this

var userd = new User()
            {
                ID = i.FirstOrDefault(),
                Type = t.FirstOrDefault(),                
                AuthorityLevel = a.FirstOrDefault(),
                Language = l.FirstOrDefault(),               
                Profiles = pro,               

            };


public IEnumerable<SelectedProfiels> profiles { get; set; }

public class SelectedProfiels
{
    public int Identifer { get; set; }
    public string ProfileName { get; set; }
}

I then pass userd to a new page:

return RedirectToAction("test", "Account", userd);

 public ActionResult test (Userm)
        {
            ViewBag.d = m.profiles;

            return View();
        }

any help please

Mich
  • 151
  • 1
  • 1
  • 18
  • Reference the `s.ProfileName` property in your `select` – Mark C. Jun 18 '15 at 13:01
  • Just a word to the wise, please [don't](http://stackoverflow.com/questions/4766062/is-using-viewbag-in-mvc-bad) [assign](http://stackoverflow.com/questions/5637958/when-is-it-acceptable-to-use-viewbag-viewdata-in-asp-net-mvc) [things](http://stackoverflow.com/questions/8328083/heavy-use-of-viewbag) to the [ViewBag](http://stackoverflow.com/questions/11262034/mvc-viewbag-best-practice) – Dylan Corriveau Jun 18 '15 at 13:03

2 Answers2

1

So, given your linq query:

 var pro = from s in 
           selectedProfiles 
           select s;

You can use Linq to generate an IEnumerable from pro, like:

var selectedProfiles = pro.Select(p => p.ProfileName).ToList();

Then, you can use selectedProfiles to populate the array you desire:

string[] newStrings = selectedProfiles.ToArray();
Mark C.
  • 6,332
  • 4
  • 35
  • 71
  • I understand what you have put here full but I get cannot implicitly convert 'generic list to IEnumerable – Mich Jun 18 '15 at 14:01
  • Since you're only interested in the `ProfileName`, did you try to just make `profiles` be `IEnumerable`? – Mark C. Jun 18 '15 at 14:02
  • I have added extra code as to how the collection is built – Mich Jun 18 '15 at 14:05
  • this 'string[] newStrings = selectedProfiles.ToArray();' works outside of the 'var used = new user()' if say i try 'Profiles = selectedProfiles.ToArray();' – Mich Jun 18 '15 at 14:09
  • I don't see how `User` is involved with this at all. Your question was very explicit, how to get the `ProfileName` values from an `IEnumberable` into an array..This is the code that does it. If something else doesn't work then that's another question. – Mark C. Jun 18 '15 at 14:11
  • yes but im want to pass back userd to to the return method without the profiles being within this passing them in to a need array is ok but it needs to be within userd – Mich Jun 18 '15 at 14:17
  • It sounds like you need to add a property on your ViewModel for the array (or another List) – Mark C. Jun 18 '15 at 14:20
  • I think I am finally getting there :) – Mich Jun 18 '15 at 14:25
  • Good! I'm glad to hear it. I hope you do get it. – Mark C. Jun 18 '15 at 14:25
  • I am thanks a lot closer to doing what I want it to do now so big thanks :) – Mich Jun 18 '15 at 14:52
0

Instead of

select s

Just use

select s.ProfileName
LInsoDeTeh
  • 1,028
  • 5
  • 11