1

I've two tables as Models in my project Like:-

public partial class TblAlbum
{
    public int ID { get; set; }
    public string Album { get; set; }
    public string Artists { get; set; }
    public Nullable<int> AudioID { get; set; }
    public Nullable<int> VideoID { get; set; }

    public virtual TblVideo TblVideo { get; set; }
    public virtual TblAudio TblAudio { get; set; }
}

and

public partial class TblAudio
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Alt { get; set; }
    public string Artist { get; set; }
    public string Image { get; set; }
    public int LangID { get; set; }

    public virtual TblLanguage TblLanguage { get; set; }
    public virtual ICollection<TblAlbum> TblAlbums { get; set; }
}

Now I've Made a ViewModel as GetDetailsVM that have access to both tables and has the LINQ Query as:-

public class GetDetailsVM
{
    private MusicEntities db = new MusicEntities();

    public IEnumerable<dynamic> GetAudio()
    {
        var AudioList = from au in db.TblAudios
                        join al in db.TblAlbums on au.ID equals al.AudioID into ar
                        from al in ar.DefaultIfEmpty()
                        select new { au,al };

        return AudioList.ToList();

    }
}

My ViewModel(AudioAlbumVM) to read Getaudio() should be something like this:-

public class AudioAlbumVM
{
    public IEnumerable<dynamic> AudioObjList { get; set; }
    public string AlbumName { get; set; }
}

Now I want to access this ViewModel in my controller and then use it in my cshtml.

My Controller:-

public ActionResult Audio() 
{
    ViewBag.Title = "Audio";

    var AudioSummary = new GetDetailsVM();
    var viewModel = new AudioAlbumVM
    {
        AudioObjList = AudioSummary.GetAudio().First()
    };

    return View(viewModel);
}

UPDATE
My View(Audio.cshtml) is as follows:-

@model GarhwalMusic.Model‌​s.AudioAlbumVM.AudioObjList

 <a class="art" href="single.html"> @Model.AudioObjList</a>

I was going through this question LINQ left join only works in the ActionResult but I'm completely lost . Need help and explanation on how to create AudioAlbumVM using another ViewModel(GetDetailsVM) then in controller and then in cshtml. Any help is greatly appreciated!!

Community
  • 1
  • 1
Deepak
  • 376
  • 6
  • 23
  • create a viewmodel with the properties that you need to use in view, and then populate the viewmodel from the result of GetAudio method – Ehsan Sajjad Dec 14 '16 at 17:07
  • 1
    show the definition of ``AudioAlbumVM``class – Ehsan Sajjad Dec 14 '16 at 17:08
  • In linq you need to select the required data into the `AudioAlbumVM` viewmodel.. So if you can provide details on that class we can help further – Rajshekar Reddy Dec 14 '16 at 17:31
  • Thanks for your comments . I'm sorry wasn't able to format my question properly before. I've updated the question. There is my viewmodel AudioAlbumVM. Hope I can make myself clear this time. – Deepak Dec 15 '16 at 02:23
  • @Deepak `IEnumerable AudioObjList` here what do you want to fill in place of `dynamic` ?? – Rajshekar Reddy Dec 15 '16 at 16:52
  • I want to get the result from getdetails.getaudio() and use them in my razor view as IEnumerable<> – Deepak Dec 15 '16 at 23:50
  • @rajshekar Any Thoughts? – Deepak Dec 16 '16 at 02:52
  • Now I'm getting this error:- The model item passed into the dictionary is of type 'GarhwalMusic.Models.AudioAlbumVM', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[GarhwalMusic.Models.AudioAlbumVM]'. – Deepak Dec 16 '16 at 02:53

1 Answers1

0

Taking this from your comments

Now I'm getting this error:- The model item passed into the dictionary is of type 'GarhwalMusic.Models.AudioAlbumVM', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[GarhwalMusic.Model‌​s.AudioAlbumVM]'.

Problem is with this code

    var viewModel = new AudioAlbumVM //its a single model
    {
        AudioObjList = AudioSummary.GetAudio().First()
    };

But it seems like your view is expecting a model of type IEnumerable<GarhwalMusic.Model‌​s.AudioAlbumVM> or (List<GarhwalMusic.Model‌​s.AudioAlbumVM>). You need to either pass a List<GarhwalMusic.Model‌​s.AudioAlbumVM> to your view or change your view to accept a model of type GarhwalMusic.Model‌​s.AudioAlbumVM.

But looking at your code I assume that you are trying to pass the collection of AudioObjList into your View. So your view must accept a model of type

GarhwalMusic.Model‌​s.AudioAlbumVM.AudioObjList and you need to just Pass in only the viewModel.AudioObjList from your controller.

EDIT 1: After you added more details of your view, You are making a mistake in the way you are accessing the data.

your Model.AudioObjList is of type IEnumerable<dynamic> and how can you just print something on the Vie like @Model.AudioObjList ?? I am referring to this code

<a class="art" href="single.html"> @Model.AudioObjList</a>

You need to typecast this dynamic type into what ever object you have created in your linq and then extract its property value. Something like

@((yourObject)Model.AudioObjList[0]).propertyName

(yourObject)Model.AudioObjList[0] type casts your dynamic data into a type yourObject.

you need [0] because your AudioObjList is of type IEnumerable<dynamic>. So taking the first data.

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
  • Thanks for your reply. In my controller I changed return View(viewModel); to return View(viewModel.AudioObjList); and my view now accept @model GarhwalMusic.Model‌​s.AudioAlbumVM.AudioObjList. But I'm getting this error now:- Cannot implicitly convert type '<>f__AnonymousType2' to 'System.Collections.Generic.IEnumerable' – Deepak Dec 16 '16 at 13:34
  • @Deepak to see the reason you might have to post your view code and part where you get this error at.. – Rajshekar Reddy Dec 16 '16 at 13:41
  • I'm sorry I'm like learning MVC right now so some concepts are new to me. Here I can't figure out how to fill 'yourObject' in @((yourObject)Model.AudioObjList[0]).propertyName. And how can I access it? PS: It seems I'm never gonna solve this. :( – Deepak Dec 17 '16 at 14:07
  • I tried to make a list object as well :- @{ List AAVM= ((au)Model.AudioObjList[0]).Album as List; } @foreach(var item in AAVM) { @item.AudioObjList } Here 'au' is selected in linq in Model GetDetailsVM. – Deepak Dec 17 '16 at 14:13
  • I've highlighted it in my question above also. var AudioList = from au in db.TblAudios join al in db.TblAlbums on au.ID equals al.AudioID into ar from al in ar.DefaultIfEmpty() select new { au,al }; – Deepak Dec 18 '16 at 16:15
  • @Deepak I would suggest you to remove `dynamic` object type and have a defined class instead. like `public IEnumerable AudioObjList { get; set; }` then in your linq you can extract values into specific type on `select` clause. – Rajshekar Reddy Dec 19 '16 at 06:23