Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get value of DataModel field in code behind without using an Entry control on Xaml page in MAUI C#?

I’m new to C# and MAUI – so thank you for your help!


What I have:

I’ve created a very simple 2 page application that works great:

  1. Page 1 uses a CollectionView to display a list of names defined by MyDataModel (FirstName, LastName, ID). If the user selects an item in the list, then the program navigates to a detail page for that item.
  2. Page 2 displays the FirstName and LastName of the selected item using {Binding} to fill <Entry /> controls, plus a Submit button to commit any changes to my database. I do not want to use an <Entry /> to display the ID field to the user.

What I need:

On my details page: How do I reference the value of MyDataModel’s ID if I don’t have an <Entry /> for it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example:

MyDataModel MyRecord = new MyDataModel();
MyRecord.FirstName = FirstNameEntry.Text;   // Value from Xaml Entry
MyRecord.LastName = LastNameEntry.Text;     // Value from Xaml Entry
MyRecord.ID = __________;                   // How do I get this value without an Xaml Entry?

More detailed code below.


Data Model

Simple data model with 3 fields per record

public class MyDataModel
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Collection View Page Code Behind

Collection View Selection Changed

private async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.CurrentSelection.FirstOrDefault() != null)
        {
            MyDataModel MySelectedItem = e.CurrentSelection.FirstOrDefault() as MyDataModel;
            await Navigation.PushAsync(new DetailPage
            {
                BindingContext = MySelectedItem 
            });
        }
    }

Detail Page Xaml

<Entry /> fields on Detail Page in Xaml

<Entry 
    x:Name="FirstNameEntry"
    Text="{Binding FirstName}" />

<Entry 
    x:Name="LastNameEntry"
    Text="{Binding LastName}" />

Detail Page Code Behind

How to access value of ID for this record without a Xaml <Entry /> for it?

public DetailPage()
    {
        InitializeComponent();
        BindingContext = this;
    }

private async void Submit_Button_Clicked(object sender, EventArgs e)
    {
        MyDataModel MyRecord = new MyDataModel();
        MyRecord.FirstName = FirstNameEntry.Text;
        MyRecord.LastName = LastNameEntry.Text;
        MyRecord.ID = ___How Do I Get ID Without Using an XAML Entry?___;   // int.Parse(IdEntry.Text);
        await SaveRecord(MyRecord);
    }

Thank you

>Solution :

pass the model via the constructor

await Navigation.PushAsync(new DetailPage(MySelectedItem));

then in DetailPage

private MyDataModel model;

public DetailPage(MyDataModel data)
{
    InitializeComponent();
    
    BindingContext = model = data;
}

private async void Submit_Button_Clicked(object sender, EventArgs e)
{
   // you are using data binding, so you do not
   // explicitly need to set any property values
   await SaveRecord(model);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading