Update Viewmodel

How can one update Viewmodel? I am adding/Updating/Deleting my data. My View model consists of multiple tables eg Employee further consists of Employee Dept., Employee Designation and so on. So, after add/modify/delete, rather then fetching data from database, I am trying to simply update my View Model, so as to avoid fetching data from Database. What's the best way to update View model?

Best Answer

  • GeetSGeetS US ✭✭✭
    Accepted Answer

    Done it by creating a method in View model which is called in XAML page after Saving data.
    1. In TicketCaseViewModel ViewModel, created a method AddNextVisitDetails
    public void AddVisitDetails(LVisitCharge lvc) //Added to update Viewmodel with new added details
    {
    TicketCase.LawyerVisitCharge.Add(new LawyerVisitCharge
    {
    LawyerVisitChargeId = lvc.LawyerVisitChargeId,
    TicketCaseId = lvc.TicketCaseId,
    PerVisitCharge = lvc.PerVisitCharge,
    Description = lvc.Description
    });
    OnPropertyChanged();
    }
    2. In .xaml, used this method like below ie after saving data to DB, called method 'AddVisitDetails' to update ViewModel.
    In page Constructor, defined View Model like below:
    public TicketCaseViewModel vmTicketCase { get; set; }

        public AddNextVisit(TicketCaseViewModel tc)  //Page Constructor
        {
            InitializeComponent();
            this.vmTicketCase = tc;
            this.TicketCase = tc.TicketCase;
            dtVisitDate.NullableDate = null;
            var lawyerpq = tc.TicketCase.LawyerPriceQuote.FirstOrDefault(x => x.TicketCaseId == tc.TicketCase.TicketCaseId && x.LawyerId == tc.TicketCase.AssignedTo);
            if (lawyerpq != null)
            {
                lblVisitCharge.Text = lawyerpq.Country.CurrencyShortName;
                lblVisitCharge.BackgroundColor = Color.FromHex("2E64FE");
                lblVisitCharge.TextColor = Color.White;
                lblVisitCharge.HorizontalTextAlignment = TextAlignment.Center;
                txtVisitCharge.Text = Convert.ToString(lawyerpq.PerVisitCharge);
                txtVisitCharge.IsEnabled = false;
            }
            lblAdditionalCharge.Text = lawyerpq.Country.CurrencyShortName;
            lblAdditionalCharge.BackgroundColor = Color.FromHex("2E64FE");
            lblAdditionalCharge.TextColor = Color.White;
            lblAdditionalCharge.HorizontalTextAlignment = TextAlignment.Center; 
        }
    

    private async void btnSubmit_Clicked(object sender, EventArgs e)
    {
    .....................
    var result = await tcService.SaveLVisitCharge(lvc);
    if (result > 0)
    {
    //Refresh data without going to DB
    if (vmTicketCase != null)
    {
    vmTicketCase.AddVisitDetails(new LawyerVisitCharge
    {
    LawyerVisitChargeId = result,
    TicketCaseId = lvc.TicketCaseId,
    PerVisitCharge = lvc.PerVisitCharge,
    Description = lvc.Description
    });
    //vmTicketCase.AddNextVisitDetails(lvc);
    }
    //Done to refresh data
    await DisplayAlert("Message", "Visit added Successfully.", "Ok");

    }

Answers

  • GeetSGeetS USMember ✭✭✭
    Accepted Answer

    Done it by creating a method in View model which is called in XAML page after Saving data.
    1. In TicketCaseViewModel ViewModel, created a method AddNextVisitDetails
    public void AddVisitDetails(LVisitCharge lvc) //Added to update Viewmodel with new added details
    {
    TicketCase.LawyerVisitCharge.Add(new LawyerVisitCharge
    {
    LawyerVisitChargeId = lvc.LawyerVisitChargeId,
    TicketCaseId = lvc.TicketCaseId,
    PerVisitCharge = lvc.PerVisitCharge,
    Description = lvc.Description
    });
    OnPropertyChanged();
    }
    2. In .xaml, used this method like below ie after saving data to DB, called method 'AddVisitDetails' to update ViewModel.
    In page Constructor, defined View Model like below:
    public TicketCaseViewModel vmTicketCase { get; set; }

        public AddNextVisit(TicketCaseViewModel tc)  //Page Constructor
        {
            InitializeComponent();
            this.vmTicketCase = tc;
            this.TicketCase = tc.TicketCase;
            dtVisitDate.NullableDate = null;
            var lawyerpq = tc.TicketCase.LawyerPriceQuote.FirstOrDefault(x => x.TicketCaseId == tc.TicketCase.TicketCaseId && x.LawyerId == tc.TicketCase.AssignedTo);
            if (lawyerpq != null)
            {
                lblVisitCharge.Text = lawyerpq.Country.CurrencyShortName;
                lblVisitCharge.BackgroundColor = Color.FromHex("2E64FE");
                lblVisitCharge.TextColor = Color.White;
                lblVisitCharge.HorizontalTextAlignment = TextAlignment.Center;
                txtVisitCharge.Text = Convert.ToString(lawyerpq.PerVisitCharge);
                txtVisitCharge.IsEnabled = false;
            }
            lblAdditionalCharge.Text = lawyerpq.Country.CurrencyShortName;
            lblAdditionalCharge.BackgroundColor = Color.FromHex("2E64FE");
            lblAdditionalCharge.TextColor = Color.White;
            lblAdditionalCharge.HorizontalTextAlignment = TextAlignment.Center; 
        }
    

    private async void btnSubmit_Clicked(object sender, EventArgs e)
    {
    .....................
    var result = await tcService.SaveLVisitCharge(lvc);
    if (result > 0)
    {
    //Refresh data without going to DB
    if (vmTicketCase != null)
    {
    vmTicketCase.AddVisitDetails(new LawyerVisitCharge
    {
    LawyerVisitChargeId = result,
    TicketCaseId = lvc.TicketCaseId,
    PerVisitCharge = lvc.PerVisitCharge,
    Description = lvc.Description
    });
    //vmTicketCase.AddNextVisitDetails(lvc);
    }
    //Done to refresh data
    await DisplayAlert("Message", "Visit added Successfully.", "Ok");

    }

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    That is basically 1998 WinForms. Complete violation of MVVM pattern.
    You do know that your ViewModel isn't supposed to be aware of any of your View's controls, right? You're reaching in and hard updating the controls through code?! Nobody that you would actually want to work for will hire someone that code likes this.

    Please, please please - just stop. Read. Learn. Get an understanding of how we do things in this century. Then return to your project and re-vamp it to be in line with modern techniques. If you continue to drive these bad habits into your normal thinking and design it will just be that much harder to unlearn them, then you still have to learn how to do it right.

    Getting started Xamarin site page

    Creating mobile apps in Xamarin - Free ebook download - the de facto textbook for Xamarin.

    http://www.RedPillXamarin.com

    You should probably give serious consideration to Xamarin University. Its a flat rate for taking all of their courses. From intro to C# to advanced memory management in Android, to network administration. Its not free, but it is a really good value for the volume of education you get out of it. You can try to source out 30 different free YouTube videos and websites or you can take a cohesive guided course that will teach you more, better and faster. Its your education toward your career aspirations: You get what you pay for.

    1. Revisiting MVVM - The 30,000 ft view
    2. Introduction to data binding
    3. Data binding basics
    4. Getting started with MVVM
    5. https://channel9.msdn.com/Shows/XamarinShow/Introduction-to-MVVM
  • GeetSGeetS USMember ✭✭✭

    Thanks for replying. I had already reverted the code since it didn't work as per requirement. Initially, I do had tried other ways to update ViewModel(MVVM approach) but it didn't work.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @GeetS said:
    Thanks for replying. I had already reverted the code since it didn't work as per requirement.

    A little tip from years of regret. Don't revert code until you're done and QA signs off. Comment out code that isn't working. That way you can walk away for a week and come back without having to remember what you did and did not try. Having 14 different plans all there in the code, but commented out keeps you from trying the same thing over. And when you finally have to throw you hands up and post a question for help, you can show that you really have tried... and all those that will help you won't have to go through the same 14 failed efforts. Or might see that one little thing in "Plan F" that would have worked with just a little tweak.

  • GeetSGeetS USMember ✭✭✭

    Clint StLaurent, Thanks for suggestion. FYI, I had reverted the code because it was not coded by me. It was coded by someone else and I was asked to check if something like this is possible.
    I used to follow MVVM pattern and I do use to comment the code till proper output is displayed.:)

Sign In or Register to comment.