Using the Sqlsugar framework requires a reference to the NuGet package or an error will occur.
Front Page Create code:
@{
Viewbag.title = "Index";
}
<link href= "~/content/bootstrap.min.css" rel= "stylesheet"/>
<script src= "~/scripts/vue.min.js" ></script>
<script src= "~/scripts/axios.min.js" ></script>
<div id= "App" >
<table style= "Width:80%;margin:auto;" class= "Table table-bordered table-hover" >
<tr>
<th> name:</th>
<td><input type= "text" v-model= "user. UserName "/></td>
</tr>
<tr>
<th> Mobile Number:</th>
<td>
<input type= "text" v-model= "user. PhoneNumber "/>
</td>
</tr>
<tr>
<th> Password:</th>
<td><input type= "text" v-model= "user. UserPassword "/></td>
</tr>
<tr>
<th> Status:</th>
<td><input type= "text" v-model= "user. UserState "/></td>
</tr>
<tr>
<TD colspan= "2" ><button v-on:click= "Add" class= "Btn btn-info" > Add </button></td>
</tr>
</table>
<table class= "Table table-bordered table-hover" >
<tr>
<td> numbering </td>
<td> User name </td>
<td> Phone number </td>
<td> Password </td>
<td> Status </td>
<td> Operations </td>
</tr>
@foreach (var item in Viewbag.data)
{
<tr>
<td> @item. Userid</td>
<td> @item. Username</td>
<td> @item. Phonenumber</td>
<td> @item. Userpassword</td>
<td> @item. Userstate</td>
<td><a href= "/home/delete/@item. UserID "> Delete </a> <a href=" # "> Modify </a></td>
</tr>
}
</table>
</div>
<script>
New Vue ({
El: "#app",
Data: {
User: {UserName: "", PhoneNumber: "", UserPassword: "", userstate:0},
} ,
Methods: {
Add:function () {
Axios.post ('/home/insert ', {users:this.user}). Then (
res = {
Window.location.href = "/home/index"
}). catch (
Error = {
Console.log (Error);
});
}
}
});
</script>
Create a Config.CS linked database
public class Config
{
<summary>
Database connection string (private field)
</summary>
private static readonly string _connectionstring = System.Configuration.
configurationmanager.connectionstrings["Sqlserverconn"]. ToString ();
<summary>
Database connection string (public property)
</summary>
public static string ConnectionString
{
get {return _connectionstring;}
}
}
Create DBSuglar.CS
public class Dbsuglar
{
public static sqlsugarclient Getsqlsugarclient ()
{
Sqlsugarclient db = new Sqlsugarclient (New Connectionconfig ()
{
ConnectionString = config.connectionstring,//Required, database connection string
DbType = Dbtype.sqlserver,//required, database type
Isautocloseconnection = True,//default false, when you know close database connection, set to True no using or close operation
Initkeytype = initkeytype.systemtable//default systemtable, field information read, such as: whether the property is a primary key, is not an identity column, etc. information
});
return DB;
}
}
Create an entity class User.cs
public class User
{
Public list<userinfo> Show ()
{
Return Users.getall (). ToList ();
}
public bool Delete (int id)
{
return Users.delete (ID);
}
public bool Insert (UserInfo US)
{
Return Users.insert (US);
}
}
Create a home controller
public class Homecontroller:controller
{
public static Sqlsugarclient db = Dbsuglar.getsqlsugarclient ();
User US = new user ();
<summary>
Inquire
</summary>
<returns></returns>
Public ActionResult Index ()
{
Viewbag.data = db. Sqlqueryable<userinfo> ("SELECT * from UserInfo"). ToList ();
return View ();
}
<summary>
Delete
</summary>
<param name= "id" > deleted primary key </param>
<returns></returns>
Public ActionResult Delete (int id)
{
Db. Deleteable<userinfo> (). Where (IT and it. UserID = = ID). ExecuteCommand ();
Return redirecttoaction ("Index");
}
<summary>
Add to
</summary>
Public ActionResult Insert (UserInfo users)
{
VAR i= db. Insertable (Users). Executereturnbigidentity ();
Return Json (I,jsonrequestbehavior.allowget);
}
}
Overall page effect
Use Sqlsugar framework +axios write and delete cases