Xamarin MVVM Display Data From Another Model/Table/Object

Hello! New to Xamarin. What is the best way to display data I need from another table/model/object? Or don't at all?

I want to try System.Linq's Enumerable.Join but doesn't that defeat the purpose of an observable collection? I want to change things and insert records. I've been trying to use another model to group the information together but no luck.

Trying to use a carousel view with another group model wrapped around the info. Questions are coming from a working API. Thanks all.

Question
-qQuestion
-name

Answer
-pAnswer
-fQuestion
-Value
-Comments

ViewModel

            IEnumerable<QuestionModel> questions = await DataSource.GetQuestionsAsync(true);

        QuestionList.Clear();

        int k = 0;

        foreach (var i in q)
        {
            // questions for template
            QuestionList.Add(i);

            var c = k++;

            string s = (c + 1).ToString();

            var a = new AnswerModel
            {
                pAnswer = s,
                Posted = DateTime.Now,
                fQuestion = i.pQuestion,
                Value = i.Standard,
                Comments = "Commnt here"
            };

            // template of answers for each question
            AnswerCollection.Add(a);
        }

        // templates
        foreach (var i in AnswerCollection)
        {
            var n = new GroupList<QuestionModel>
            {
                pGroup = i.pQuestion.ToString(),
                Name = i.Name
            };

            n.Add(i);

            GroupedAnswerCollection.Add(n);
        }

        //var g = AnswerCollection.Join(
        //    QuestionList,
        //    foreign => foreign.fQuestion,
        //    primary => primary.pQuestion,
        //    (primary, foreign) => new
        //    {
        //        Test = primary.pAnswer,
        //        Test2 = foreign.Name,

        //    }).ToList();

Xaml


<CarouselView.ItemTemplate>



                            <StackLayout>
                                <Label Text="{Binding pGroup}"></Label>
                                <Label Text="{Binding Name}"></Label>
                                <Label Text="{Binding Other}"></Label>
                            </StackLayout>


                            <StackLayout>
                                <Label Text="{Binding pAnswer}"></Label>
                                <Label Text="{Binding fQuestion}" ></Label>
                                <Label Text="{Binding Value}" ></Label>
                                <Label Text="{Binding Comments}" ></Label>
                            </StackLayout>

                        </StackLayout>

                    </Frame>
                </StackLayout>

            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>

Best Answer

  • AaronBlaylockAaronBlaylock US ✭✭
    Accepted Answer

    Got the answer. generally you'll have a model for the object you receive from the database. This can be used to store changes and then sent back, but you would have to design your endpoints to use the same model. in your case, you want to combine questions and answers to display so you need a common model. then when you send the changes back you just need to put them into any model to send back to your endpoint
    View


    <CarouselView.ItemTemplate>




                                <StackLayout>
                                    <Label Text="{Binding pGroup}"></Label>
                                    <Label Text="{Binding Name}"></Label>
                                    <Label Text="{Binding Other}"></Label>
                                </StackLayout>
    
    
                                <StackLayout>
                                    <Label Text="{Binding pAnswer}"></Label>
                                    <Label Text="{Binding fQuestion}" ></Label>
                                    <Label Text="{Binding Value}" ></Label>
                                    <Label Text="{Binding Comments}" ></Label>
                                </StackLayout>
    
                            </StackLayout>
    
                        </Frame>
                    </StackLayout>
    
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    

    ViewModel

    private IEnumerable groupedCollection;
    public IEnumerable GroupedCollection
    {
    get => groupedCollection;
    set
    {
    groupedCollection = value;
    OnPropertyChanged(nameof(GroupedCollection));
    }
    }

    //Can pull information from multiple sources and package it for the view.
    private void GetQuestionAnswers()
    {
    //pulling data from 2 separate sources
    var questions = await QuestionApi.GetQuestions();
    var answers = await AnswersApi.GetAnswers();

    //use these to build QuestionAnswer list called questionAnswerList
    
    GroupedCollection = questionAnswerList;
    

    }

Answers

  • AaronBlaylockAaronBlaylock USMember ✭✭
    Accepted Answer

    Got the answer. generally you'll have a model for the object you receive from the database. This can be used to store changes and then sent back, but you would have to design your endpoints to use the same model. in your case, you want to combine questions and answers to display so you need a common model. then when you send the changes back you just need to put them into any model to send back to your endpoint
    View


    <CarouselView.ItemTemplate>




                                <StackLayout>
                                    <Label Text="{Binding pGroup}"></Label>
                                    <Label Text="{Binding Name}"></Label>
                                    <Label Text="{Binding Other}"></Label>
                                </StackLayout>
    
    
                                <StackLayout>
                                    <Label Text="{Binding pAnswer}"></Label>
                                    <Label Text="{Binding fQuestion}" ></Label>
                                    <Label Text="{Binding Value}" ></Label>
                                    <Label Text="{Binding Comments}" ></Label>
                                </StackLayout>
    
                            </StackLayout>
    
                        </Frame>
                    </StackLayout>
    
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    

    ViewModel

    private IEnumerable groupedCollection;
    public IEnumerable GroupedCollection
    {
    get => groupedCollection;
    set
    {
    groupedCollection = value;
    OnPropertyChanged(nameof(GroupedCollection));
    }
    }

    //Can pull information from multiple sources and package it for the view.
    private void GetQuestionAnswers()
    {
    //pulling data from 2 separate sources
    var questions = await QuestionApi.GetQuestions();
    var answers = await AnswersApi.GetAnswers();

    //use these to build QuestionAnswer list called questionAnswerList
    
    GroupedCollection = questionAnswerList;
    

    }

Sign In or Register to comment.