Relation dependencies in EntityFramework is common task. This package will create a select box, or checkboxes depending if you have a Single or Multiple relationship.
This package hooks up with MVC5.
Run the following command
Install-Package RelationScaffolding
The current package does not setup everything automatically. Please keep reading to understand what has to be done manually to use this package.
The package will install the following:
- DLL reference
- Templates in /Views/Shared/[EditorTemplates|DisplayTemplates]
- Feel free to modify those templates to your needs, note that this project is in beta and those files will most likely be updated later
- If you use this inside an Area, you do not need to move these files. They will be picked up automatically.
- One JavaScript file /Scripts/relation.js
Install the JavaScript in your bundles
BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/relation").Include(
"~/Scripts/relation.js"));
_Layout.cshtml
@Scripts.Render("~/bundles/relation")
After everything is setup, you can name your property that you want to get a relation displayed.
public class Book
{
[Key]
public long Id { get; set; }
[Required]
public string Name { get; set; }
[RelationScaffolding.RelationSingle(Empty = "Select an author")]
public Member Author { get; set; }
[RelationScaffolding.RelationMultiple]
public virtual ICollection<Member> Followers { get; set; }
}
On the Member class, you have to mention which property is the key, and which one you want to display.
public class Member
{
[Key]
public long Id { get; set; }
[Required]
[RelationScaffolding.RelationDisplay]
public string Name { get; set; }
}
If you do not set the [Key]
attribute, we try to find a property that ends with Id
.
Definition of the attributes:
RelationSingle
: will display a single property in display mode or a combo box in edit mode. Use theEmpty
option to pre-fill the combo box with a default string value.RelationMultiple
: will display an unordered list in display mode or an unordered checkbox list in edit mode. Use theCanAdd
option to add a textbox allowing addition to the list. Use theMany2Many
option to tell that the collection is a joint table.Relation
: will use theRelationMultiple
orRelationSingle
accordingly if the property is an array or not.RelationDisplay
: indicates which property to use when displayed inRelationSingle
orRelationMultiple
. You can combine this attribute with the attribute [NotMapped] if you want to add a computed property.RelationEdit
: indicates which property will be used when using theCanAdd
fromRelationMultiple
.RelationList
: indicates which property will be used as a list for a Many2Many joint table. Use the "PropertyNameList" in your cshtml to populate the whole list of item.
The current scaffolding is not automatically populating the view for relations. Use the same code as a primite would use to generate relations.
Use something like this:
@Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } })
If you are dealing with a Multiple
, you probably want to get the whole list of items. Use the following:
@Html.EditorFor(model => model.Followers, new { list = ViewBag.AllMembers, htmlAttributes = new { @class = "form-control" } })
If you are using Many2Many
, you can assign the list to the variable name "FieldName
List".
@Html.EditorFor(model => model.Followers, new { PersonList = ViewBag.AllMembers, htmlAttributes = new { @class = "form-control" } })
In your controller:
ViewBag.AllMembers = dbContext.Members.ToList();
I am actively working on this.
- Model Binder
- Make "CanAdd" more generic
- Support Many 2 Many
- Add a new Scaffolding template T4
- Better support of styles
- I18n
- Unit Tests