Passing data between pages Xamarin Shell navigation - c#

I have a Xamarin forms app with a form split across multiple pages, I want to pass the object data to the next or previous page. I am navigating using the Xamarin Shell. What method or setup can I use to achieve this?
The options I am aware of and my perceived issues with them:
JSON string the object and pass it as a parameter.
This seems incorrect as the data is being converted back and forth.
Pass every property of the object as an individual parameter.
Massively long winded with many properties and inflexible to change.
Store the data to a SQLite database.
I would not want to store an incomplete record in the table and using the current SQLiteAsyncConnection, I don't believe I can have 2 tables created from the same class.

Yes,you can pass data using query property attributes .
Navigation data can be received by decorating the receiving class with a QueryPropertyAttribute for each query parameter.
For more, check:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#process-navigation-data-using-query-property-attributes .
In addition,another method is to create a global varible in your app, them you can access this varible in your app.
For example:
1.create class MyVariables.csand add static variable for your model (e.g. MyViewModel ) :
public class MyVariables
{
public static MyViewModel myViewModel { get; set; } = new MyViewModel { Name = "test1" };
}
MyViewModel.cs
public class MyViewModel
{
public string Name { get; set; }
}
2.You can modify or access your variable in your app:
// modify the variable
MyVariables.myViewModel.Name = "test2022";
// access the variable
System.Diagnostics.Debug.WriteLine("the data is: " + MyVariables.myViewModel.Name);

Unfortunately, we could only pass simple data now. And this feature will be added in the future: https://github.com/xamarin/Xamarin.Forms/issues/6848 You can create multiple QueryPropertyAttribute to access different data: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-data Another approach is to convert the object to a JSON string like:
var jsonStr = JsonConvert.SerializeObject(model);
Then pass it when navigating.

Related

Standard way of Data Passing b/w multiple pages in WPF

I am working on a WPF project which is having multiple pages, and I have to pass multi-variable data b/w pages. Now the technique I am using to pass data is; I have created a model class for those variable as given below;
public class CAR
{
public string MODEL;
public string BRAND;
public string COLOR;
}
And when I navigate to the next page i pass the above model class object to page as given below;
{
CAR cr1 = new CAR();
//Assign values to cr1
Page2 pg = new Page2(cr1);
NavigationService.Navigate(pg);
}
Now my question is, am I following the standard way of passing data b/w pages? or there is some other proper way to pass multi-variable data. kindly guide me

MVC 4 Dynamic action method argument

I have the following class definition:
public class CallGroupViewModel
{
public string Name { get; set; }
public string BulkEmails { get; set; }
public List<dynamic> Members {get;set;}
}
The Members property is some dynamic knockout code and the contents change depending on UI stuff. I am wanting to take a short cut since the UI is so up in the air right now.
My problem is that when it get to my action method, everything gets populate, including Members but its a array of objects and I can't get at the individual properties on them.
public ActionResult SaveGroup(CallGroupViewModel group)
group.Members //this has a count
group.Members[0].email //this pukes as 'email' is not valid for 'object'
I'm probably just missing something, any help is appreciated.
Thanks!
The model binding system which populates MVC action method arguments isn't intended to work with dynamic objects. The system requires type information to know which information to bind. Consider changing your model to use strong types instead.
For accessing dynamic properties from the object, in your case you can use :
var email = group.Members[0].GetType()
.GetProperty("Email")
.GetValue(group.Members[0], null);

Pass MVC Controller a list of columns and return data for those columns?

I am trying to build a helper class for Datatables and need some advice.
I want to create a helper that I can pass in an object that represents the settings for a DataTables that is typed by a specific model. This object would contain a collection of expressions that represent the columns to be used by the helper. As part of the DataTables object there will be a property that holds the URL that servers up the data for the grid. This will effectively be the sAjaxSource parameter. As part of the URL call, a JSON package will be sent that will contains the information about the columns. This whole section, I understand how to build. Basically, I would build out the DataTables JavaScript code including column definitions and create a JSON object that represents the columns I want to pass in the URL.
The area I needs some advice on is building the data for the specific columns once hte call is made to the server / controller. So my controller may look like this:
public ActionResult GetUsersList(IEnumerable<DatatableColumnJson> columns)
{
var users = _someUserRepository.GetAll();
foreach (var user in users)
{
//Here I would build the JSON that DataTables needs to render the grid.
//I would iterate through the users and pull out the data for the specific columns
}
return Json(someCompleteDataTablesObject, JsonRequestBehavior.AllowGet);
}
Supporting Classes:
public class DatatableColumnJson
{
public string Name { get; set; }
public string Visible { get; set; }
}
public class User
{
public string UserName { get; set; }
public string Email { get; set; }
}
So I first thought I could use reflection, but that seems to be a little intensive, maybe. Another thought was to transform the column names into expressions to possibly pull out the data. I want something efficient and that doesn't have a lot of overhead, but maybe I am asking too much.

How to create a single drop down for multiple data types?

I am using ASP.Net MVC 3 and I need to create a single drop down list which contains items that relate to multiple database tables.
Normally, if I need to do a drop down list for a single data type I can easily use the ID as the "value" for each drop down option and would do something like this:
#Html.DropDownListFor(x => x.SelectedID, Model.GetMyList())
But now I want to mix up multiple data types. So lets say for this example I want to create a single list to represent something like "Owner" and this can be either a "User" or a "Customer". In this example, both User and Customer are separate database tables and therefore the ID value alone is not enough to identify them correctly.
So what are the best ways to achieve such functionality?
Straight off the top of my head, my first thoughts are to create a "custom" value string which could then be parsed server side to work out the ID and data type, something like...
"USER|1"
"CUSTOMER|1"
I know I can make this work, but am I making this more complicated than it needs to be? Is there a built-in or advised way of doing this?
In your Model can you not do something like this:-
public class Model
{
public string Owner { get; set; }
public List<MyList> ListCollection { get; set; }
public class MyList
{
public int Id { get; set; }
public string Value { get; set; }
}
}
So then when you are checking which list item is selected you also have access to the "Owner" field which will tell you what table it belongs to ?
As nobody has come up with anything better, I can confirm that my original idea (as unwanted as it was) did the job.
When setting the value of the select options, a custom string should be created that can easily be parsed server side, this was achieved using a pipe separating the TYPE of entity, and the ID, for example:
"USER|1"
"USER|2"
"CUSTOMER|1"
"CUSTOMER|2"
Once the selected value is passed to the server, it can then be parsed something like the following:
string option = "USER|1";
string[] values = option.Split('|');
string entityType = values[0];
int entityId = Int.Parse(values[1]);
which can then be used something like this:
if(entityType == "USER")
UpdateUser(entityId);
else//CUSTOMER
UpdateCustomer(entityId);

Editing form fields via ajax and .net mvc

I have a number of inputs on my page. I would like to save the changes to the model on the input blur, so as I change the value of each input it gets saved back to server, like Google contacts.
<input id="FirstName" name="FirstName">Jack</input>
I create a blur event using jquery to post the value back to the server. It posts a structure with the name of the input, the value and an id of the entity.
$.post(url, { id: "2", key: "FirstName", value: "Jack" }, successFuction);
In my controller I have:
public ActionResult EditField(int id, string key, string value)
I then retrieve the entity using EntityFramework with the id. I then wanted to update the property on the model for the field.
var entity = _db.Get(id);
entity[key] = value;
return Content "Success";
Which I obviously can't do! The only way I can think off is multiple methods for each field so EditName, EditAddress etc. which seems wrong. I want this method to be able to handle each property of the model.
What is a better way to structure the controller instead of writing multiple methods for each individual field?
You could post your entire form (e.g. first name, last name, etc.) on each blur for any of your fields (this should be fine since you're saving all changes as the user progresses on the form anyway). Unless you're really trying to save bytes, posting the whole form seems fine.
You could just post the field name and then use reflection to look up the property of your object and set the value.
I think that you can do it if you are willing to model the entity in a general way:
public class FieldEntity {
public int Id { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
Then use it inside the context like:
var fieldEntity = db.Find(id);
fieldEntity.Key = key;
fieldEntity.Value = value;
db.SaveChanges();
However, it is usually better to structure data in a way that is meaningful. In the example you describe it looks like you might have a Person and Address entity. So why not have a Person entity that has a property Address?

Categories

Resources