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

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

Related

Passing data between pages Xamarin Shell navigation

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.

How to display values from controller?

public ActionResult Dashboard()
{
var data = Processor.LoadBarangays();
List<BarangayModel> barangays = new List<BarangayModel>();
**int totalConfirmed = 0;
int totalPUI = 0;
int totalPUM = 0;**
foreach (var row in data)
{
***totalConfirmed = +row.Confirmed;
totalPUI = +row.PUI;
totalPUM = +row.PUM;***
barangays.Add(new BarangayModel
{
Barangay = row.Barangay,
Confirmed = row.Confirmed,
PUI = row.PUI,
PUM = row.PUM
});
}
return View(barangays);
}
I have the above code for displaying this table: red mark now I added the codes in emphasis above so that I can show it in the cards in blue mark: blue mark. How can I display the values of the variables in the view? Can I use multiple views in a single controller? Thank you.
It seems like what you are looking for is show multiple views in a single view, as from the controller you can only return one, in your case Dashboard (when you dont specify, they have to be the same name as the method)
You can do that with partial views. and it depends a little bit in which version you are, if .NET framework, net core etc, also If you are using razor or any other framework for the front end.
if you are using razor if you type #HTML.Partial("viewname") will load a partial view inside your "main" view. and you can send a model (an object) to it as second parameter #HTML.Partial("viewname", model)
for your example, you're returning return View(barangays); which is the red box.
then to include the blue ones what you have to do is expand your model. so your BarangayModel should become a DashboardModel and one of the properties has to be List<BarangayModel> .
class DashboardModel{
public List<BarangayModel> BarangayModelList {get;set;}
}
the reason to do this is because you have to add now, your red box. lest call the object PersonStatus and the example will be like:
class PersonStatus{
int confirmed {get;set;}
int recovered {get;set;}
//rest of the properteis
}
and then you expand your DashboardModel to contain this new object:
class DashboardModel{
public List<BarangayModel> BarangayModelList {get;set;}
public PersonStatus PersonStatus {get;set;}
}
so in your Dashboard.cshtml now you need to specify the new model.It is common to be in the first line of the view. And it looks like this #model List<BarangayModel> you will need to update it to #model DashboardModel
as you change for List<BarangayModel> to the new one, the foreach to print the table will fail. which means that you probably have something like foreach(var bm in Model) now you have to update it to foreach (var bm in Model.BarangayModel)
now you have both informations on the view, but you are only printing the BarangayModel
what you have to do is in the same folder as this view (or in the Shared one) create a view, which will receive the new PersonStatus object as parameter.
then you need to call it as a partial view from your main view, passing the parameter:
#Html.Partial("PersonStatusView", Model.PersonStatus);
or
<partial name="PersonStatusView" model="#Model.PersonStatus);
depends of which version/framework you're running.

Need to catch JSON object returned from Controller and display as HTML in .cshtml file(View)

I need to display the data returned from my controller class of type JSON in HTML table on View.
Below are the details.
Controller class
ROCRespository rocrep = new ROCRespository();
public JsonResult ROC()
{
return Json(rocrep.GetAllRocDetails(),JsonRequestBehavior.AllowGet);
}
Model Class:
public class ROCRespository
{
public List<ROC> roc;
public ROCRespository()
{
this.roc = new List<ROC>()
{
new ROC(){CourseId="1",CourseName="Softskill 321 : Client Interaction through conference call",CourseImagePath="../../Content/Images/php.png"},
};
}
public List<ROC> GetAllRocDetails()
{
return roc;
}
}
I need to display the details in my view as HTML table.
Edit:
Currently I am getting an output like,
[{"CourseId":"1","CourseName":"Softskill 321 : Client Interaction through conference call","CourseImagePath":"../../Content/Images/php.png","rating":0}]
Need to display in a neatly designed table or div's
You can use Jquery datatables that accepts the JSON as datasource. See this sample which uses the Json source to create a HTML table with extended grid functionalities.
Or
You can build your custom HTML from JsonResult like this and this.

C# - Entity type (entity) not part of model for current context error in Windows Form Application with SQL Database

I'm working on a project that involves simply filling out a string of textboxes and clicking a button that will add the values in the textboxes to their respective variables in a new instance of a class, and save this instance of the class in a database.
I created the database first of all and used Entity Frameworks to create the class from it, so I know they are linked to eachother.
The general code for my class is shown below:
public partial class AnaestheticRecord
{
public int PatientID { get; set; }
public System.DateTime Date { get; set; }
public string Owner_Name { get; set; }
//25 other attributes of class
...
//methods------------------------------------------------
//get number of instances of class (objects + 1 creates new PatientID)
private static int objects = 0;
public AnaestheticRecord()
{
++objects; //add one to count
}
~AnaestheticRecord()
{
--objects; //remove one from count
}
public static int getPatientID()
{
objects += 1; //add 1 more to create previously unused value
return objects; //return new value
}
So, when it came time to add the code to make the application function, I started by declaring new instances of the class displayed above, and of the Entities class that should allow access to the database...
public partial class Form1 : Form
{
//empty instance of class, each property takes data through console input
AnaestheticRecord aRecord = new AnaestheticRecord(); //create new instance of anaesthetic record class ready for data input
List<AnaestheticRecord> recordCollection = new List<AnaestheticRecord>(); //create list to store instances of anaesthetic records (display-record purposes)
BlueBookDBEntities db = new BlueBookDBEntities(); //create entities to run database
This all works fine, but once the series of textboxes has been filled out and the button is clicked to add this record to the datase, the following code is run and an error is produced:
private void btnSaveRecord_Click(object sender, EventArgs e)
{
//transfer and convert values from input to respective field in record
addToClass();
recordCollection.Add(aRecord); //add completed instance of class to list
db.TblAnaestheticRecords.Add(aRecord); //add completed anaesthetic record to database (ERROR)
db.SaveChanges(); //save new input to database
}
At this point, the line 'db.TblAnaestheticRecords.Add(aRecord)' throws an error stating that "Entity type AnaestheticRecord is not part of model for current context".
I'm a little stuck here, as I can't see what I've done wrong. I've done this kind of thing before using MVC where slightly more of the groundwork is done for you, but this is my first time using a database linked to a Windows Form Application.
If anyone could point out to me where I've gone wrong, and maybe point me in the right direction as to how to get past this error, I would really appreciate it.
Thanks,
Mark
In the context you need to insert also the AnaestheticRecord DbSet (all the related tables, 1 DbSet for relational table)

How do I call different mapping strategy for same type in AutoMapper?

I have a customer class & I'd like to have the customer emails field display in CSV format for an admin list view. When the edit view shows, I'd like to have the view show emails in a textbox split with a newline character feed.
I don't want to have 2 different classes for each view just use the same, with the first using IEnumerable<T> to display the list. My class is quite large and I don't want to have 2 separate view classes to manage.
The ideal goal would be to have 2 different AutoMapper mappings 1 for each different mapping scenario rather than just being limited to the the one created using CreateMap at bootstrap stage. How do I achieve this? Effectively I'd like to switch the mapping strategy depending on where I am in the code.
Ideally you would use two different View Models and map the source to the desired destination. However, if you don't want to go this route one option would be to create a view model that has two readonly properties.
public class SomeClassViewModel
{
public string FirstName { get; set;}
public string LastName { get; set; }
public string Emails { get; set; }
public string EmailsCSV
{
get
{
var csv = Emails;
//Do CSV transform here
return csv;
}
}
public string EmailsCRLF
{
get
{
var crlf = Emails;
//Do crlf transform here
return crlf;
}
}
}
Again, ideally you would want to stick with the rule of one model per view. That doesn't mean you are required to write an entirely new view for each model, there's always inheritance.
MyViewModelA : MyViewModelBase
MyViewModelB : MyViewModelBase

Categories

Resources