It's very simple to build a basic VM. You only need to inherit the BaseVM. In the following sample, we will build a LoginVM to handle user login operations.
using WalkingTec.Mvvm.Core;
public class LoginVM : BaseVM{
}
The above code defines a LoginVM. What's the use of it?
In short, when you create this VM in the Controller through the CreateVM method, the framework will automatically transfer a lot of data needed for writing logic to the VM, such as the information of the current login user, the information of form submission, the current database connection, etc
At the same time, the properties in VM can also be used to bind the front-end page controls. VM connects the front-end, back-end and database. In VM, you can access all the required information to write the logic you need.
Let's improve LoginVM, below is a demonstration of a simple login operation
using WalkingTec.Mvvm.Core;
public class LoginVM : BaseVM{
[Display( Name = "Account")]
[Required( AllowEmptyStrings = false)]
[StringLength(10)]
public string Username { get; set; }
[Display( Name = "Password")]
[Required( AllowEmptyStrings = false)]
[StringLength(10, ErrorMessage = "{0} has maximun {1} characters")]
public string Password { get; set; }
public LoginUserInfo DoLogin()
{
//use itcode and password to query a user
var user = DC.Set<FrameworkUserBase>()
.Where(x => x.ITCode.ToLower() == ITCode.ToLower() && x.Password.ToLower() == Password.ToLower() && x.IsValid == true)
.SingleOrDefault();
//output error if not found
if (user == null)
{
MSD.AddModelError("", "Login failed");
}
return user;
}
}