ViewModels
We want to have more control over the Orders
rest API:
- We want to make the Order ID automatically set for new rows.
- We want to validate that the customer id exists.
- We want to validate that the shipper id exists.
To do that we'll create a View Model and manipulate it.
Adding a view model
We'll add a folder called ViewModels
under the MVC
root folder and in it we'll add a new item using the ViewModel
template and call it OrdersViewModel
using System;
using System.Collections.Generic;
using System.Text;
using Firefly.Box;
using ENV;
using ENV.Web;
namespace MVC.ViewModels
{
class OrdersViewModel : ViewModel
{
public OrdersViewModel()
{
}
}
}
Register the OrdersViewModel
instead of the Orders
entity in the Controllers\DataApiController.cs
static DataApiController()
{
_dataApi.Register(typeof(Northwind.Models.Categories),true);
- _dataApi.Register(typeof(Northwind.Models.Orders),true);
_dataApi.Register(typeof(ViewModels.OrdersViewModel));
_dataApi.Register("orderDetails",typeof(Northwind.Models.Order_Details),true);
_dataApi.Register(typeof(Northwind.Models.Customers));
Set the Orders
as the From
of the ViewModel
class OrdersViewModel : ViewModel
{
Northwind.Models.Orders Orders = new Northwind.Models.Orders();
public OrdersViewModel()
{
From = Orders;
}
}
Make the ViewModel
updatable
class OrdersViewModel : ViewModel
{
Northwind.Models.Orders Orders = new Northwind.Models.Orders();
public OrdersViewModel()
{
From = Orders;
AllowUpdate = true;
AllowInsert = true;
AllowDelete = true;
}
}
- Automatically Generating Order Id on a new row
- Server side Validation made easy
- Controlling which columns are included in the api
- Server Side Expression
- Filtering data on the server
- Server side Relation
- Server Side Validation
- Restricting which columns can be updated in the web api
- Saving and loading Entity Data using JSON
Help us improve, Edit this page on GitHub
or email us at info@fireflymigration.com