Let's say I have a class Person:
public class Person
{
public string Name {get; set;}
public int Age {get; set;}
}
I would like to create some sample data in Blend to help me design my user interface visually. I choose to create sample data based on a class in Blend, but what I get is a sample Person - singular. I want to create a collection of Person to bnd to a list box. How do I tell it to do this? I can't find anywhere where it asks. Do I have to create a class that is a collection of Person. Surely there has to be a way to do this?
Thanks in advance.
I found a way to do this, though not ideal.
The creation of sample data based on a class is a one-time thing. Here's what I did to get my list of Person objects in sample data:
public class Person
{
public string Name {get; set;}
public int Age {get; set;}
}
public class PersonCollection : List<Person> {}
I created the PersonCollection class, which is simply a collection of Person objects. I then created my sample data based on the PersonCollection class - giving me the sample data I was after. I then removed the PersonCollection, leaving the sample data in place.
I'd call this a workaround rather than a solution. If anyone can offer a true solution - a way to do this in Blend without having to create summy classes, I'll be more than happy to mark that as the solution.
You can use data pane->Add sample datasource->Define New Sample Data to do this.
Related
I have realm object Item with linked collection Properties:
public class Item : RealmObject
{
public IList<Property> Properties { get; }
public int Id {get; set;}
....
}
public class Property : RealmObject
{
public string Key {get; set;}
public string Value {get; set;}
}
And I need to sort to Item entities by properties from linked Properties collection.
Something like this (I know it's not supported by Realm):
Realm.All<Item>().OrderBy(f => f.Properties.FirstOrDefault( p => p.Key == "Status").Value)
It is not possible to move properties to Item entity cause different items can contain different properties set which also may change over time.
Is there other options to implement this kind of sorting? For now I see only option to sort objects in memory, but it may take too much memory on big dataset.
This is not yet possible, I'm afraid.
We're going to be working on many LINQ improvements soon, but this particular request is on the complex side so it probably won't be something we will be able to offer in the near future.
I let VS create my model from existing SQL tables using EF 4.3.1. Because the DB tables are not final and may change I don't want to set the validation attributes directly into the model file because it gets overwritten when I update the model.
I want to use the MetadataTypeAttribute approach described here:
Update Model From Database (Database First)
So I created an external file containing this class:
using System.ComponentModel.DataAnnotations;
namespace PDB.Models
{
[MetadataTypeAttribute(typeof(t_scriptingMetadata))]
public partial class t_scripting
{
}
public class t_scriptingMetadata
{
[Required]
public int platform {get; set;}
[Required]
[StringLength(20)]
public string Projectname {get; set;}
}
}
Unfortunately I get these errors in VS:
Duplicate EdmEntityTypeAttribute attribute
Duplicate Serializable attribute
Duplicate DataContractAttribute attribute
My model that has been generated by VS has exactly these attributes the error message mentions:
[EdmEntityTypeAttribute(NamespaceName="CAWI_STDMGTModel", Name="t_scripting")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class t_scripting : EntityObject
{
//...
}
What am I doing wrong?
Your t_scriptingMetadata class have to be into t_scripting not necessarily
[MetadataTypeAttribute(typeof(t_scriptingMetadata))]
public partial class t_scripting
{
public class t_scriptingMetadata
{
[Required]
public int platform {get; set;}
[Required]
[StringLength(20)]
public string Projectname {get; set;}
}
}
And both partial classes must have same namespace.
Hope this help.
Regards
I have found the problem. I had a table in my model that actually is a view containing fields from another table that was also in my model. So the error messages were indeed right. Thank for your help, vfabre!
I have two database classes as defined below:
public class TopDate
{
[Key]
public int DateId { get; set; }
public DateTime Date { get; set; }
}
public class TopSong
{
[Key]
public int SongId { get; set; }
public string Title { get; set; }
public int DateId { get; set; }
}
where DateId is foreign key to TopSong
I am creating a controller through which i can create, delete or edit these database values.
When i right click on controller class and add controller i can only select one of the two classes defined above. Is there a way to make 1 controller to handle database updates to both these tables on one page?
Error Image:
Your controller should not be dealing directly with domain objects (meaning those things that are directly associated with your database). Create a ViewModel that contains the properties that you need, use your service layer to populate the ViewModel and your controller will use that as the Model for its base. An example of your ViewModel could be something like the following given your description above:
public class MusicViewModel
{
public int SongId {get;set;}
public string Title {get;set;}
public IEnumerable<DateTime> TopDates {get;set;}
}
This view model would contain a list of all dates that a specific song was a Top Song.
The objects you showing (code) are database classes (so called domain objects).
What you need to do is to define a view model, a standard ASP MVC practice:
you define a class, that is tailored for specific view and only containing data relevant to that particular view. So you will have a view model for a view that will create a song, another that will update it etc.
Actually situation you describing is classical situation to use view models. Using domain objects in the views, however, is really really bad practice and prone to more problems than you want to deal with.
Hope this helps.
I am working on an mvc3 application and having some problems with getting validation to work as I want.
The application is using buddy classes for the models. (This is something I haven't used in the past and I am a little confused why they are used...anyway)
I want to add required fields to ensure data been submitted is correct. I have tried adding the required field to the buddy class.
When I submit the form no client-side validation takes place and the debugger steps into the entity frameworks generated code. Here is complains that the fields that contain null values are causing are invalid. If I step through all of those it finally gets to the controller where my if (ModelState.IsValid) is showing false.
I have client-side validation switched on.
Am I meant to be applying the data validation at the buddy class level or at the view model?
One other question is why use buddy classes? to me they seem to over complicate things.
Updated added an example of the buddy class
[MetadataType(typeof (CustomerMetaData))]
public partial class Customer
{
public string Priorty
{
get
{
var desc = (Priority) Priority;
return desc.ToString().Replace('_', ' ');
}
}
internal class CustomerMetaData
{
[Required]
[DisplayName("Priorty")]
public string Priorty { get; set; }
Buddy classes are metadata classes to put data annotation attributes when you are not in control of the original class i.e. can't edit it. Typical situation is when the class is generated by an ORM like Entity Framework.
//Can't edit this class
public partial class YourClass{
public string SomeField {get; set;}
}
//Add a partial class
[MetadataType(typeof(YourClassMetadata))]
public partial class YourClass{
}
//And a metadata class
public class YourClassMetadata
{
[Required(ErrorMessage = "Some Field is required")]
public string SomeField {get; set;}
}
are you sure that you have [MetadataType(typeof(YourClassMetadata))]?
More about buddy classes here and here
You would typically use a buddy class when it isn't possible to add meta data to an entity class such as when a model is automatically generated by an ORM tool. In this case any meta data you had applied would be lost.
Therefore, your original (automatically generated) class would be defined as a partial class:
public partial class Customer
{
public string Priority { get; set; }
}
And then you would generate your buddy classes to add the meta data.
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}
internal class CustomerMetaData
{
[Required]
public string Priority { get; set; }
}
You would then pass the Customer class to the view where the Priority would be set.
In your case i'm not sure if you only have one partial class or two (as the other is not shown but please provide if there is). I'm interested to know how you obtain the priority information from the customer as i'm wondering if this is an issue with how you use ModelState.IsValid? The reason I ask is that no set accessor is declared on the Priority property so i'm wondering how this is set from the view in order to report that it is not valid?
You would also use a buddy class when it isn't possible to add meta data to an entity class such as when a model is automatically generated by an WCF Data Contract.
For example, in an IDE application, say for C#, there are 2 views ClassView and TextView.
In ClassView we need to display the Class information in a hierarchical manner, where as in TextView the code for that Class is shown.
The ClassView needs to query for classes, methods, properties, fields, etc. whereas the Text View queries lines in the code.
Both the views are editable. Change in one view should be reflected in the other view too.
So Class View requires one model and Text View requires another but the underlying data is the same.
Is there a design pattern to solve such a problem?
Thanks in advance.
Certainly an MVC model can be hierarchical. If your data is all contained in a single file, maybe you don't really need multiple models for your application? How about:
namespace MyNamespace
{
public class CodeFile
{
/// <summary>
/// A list of contained classes for the Class view
/// </summary>
public List<CodeClass> Classes { get; set; }
public CodeFile()
{
Classes = new List<CodeClass>();
}
public string ToString()
{
// send output to Text view
}
}
public class CodeClass
{
public string ClassName {get; set;}
public List<CodeProperty> Properties {get; set;}
public List<CodeMethod> Methods {get;set;}
public CodeClass(string className)
{
ClassName = className;
Properties = new List<CodeProperty>();
Methods = new List<CodeMethod>();
}
}
public class CodeMethod
{
public string MethodName { get; set; }
}
public class CodeProperty
{
public string PropertyName
}
}
Model View Controller :)
The mistake in your question is that your data is actually mapped on your model.
You can have 2 views (classview and textview) and they both works with one single common model. Controller can update one view when another one changes the model.
You tagged it yourself with MVC... The underlying data is the model, the Class View and Text View act as views/controllers. The model sends out events to its views, to make sure that changes in one view are reflected in the other.
There's nothing in MVC architecture to prevent writing multiple model hierarchies which interact with the same underlying data store.
You would just have Controllers/Views which interact with Model A, and different Controllers/Views which interact with Model B.