-----------API section//Hook up API
public class Productcontroller:apicontroller
{
Aentities db = new aentities ();//reference db
Public ienumerable<zuohebiao> Get (string name, int id)
{
list<zuohebiao> list = new list<zuohebiao> ();//Instantiate a combination class to receive data
var AA = db. Xitong.tolist ();//Information table
var BB = db. Typeinfo.tolist ();//Type table
List = (from a in AA//Using a LINQ connection query
Join B in BB
On A.typeid equals b.id
Select New Zuohebiao
{
ID = a.ID,
Door = A.door,
TypeName = B.typename,
Moneyinfo = A.moneyinfo,
Getmoneyman = A.getmoneyman,
Timeinof = A.timeinof,
Typeid=a.typeid
}
). ToList ();
if (!string. IsNullOrEmpty (name))//judgment
{
list= list. Where (P = = p.getmoneyman.contains (name)). ToList ();//Replace List
}
if (ID > 0)
{
List = list. Where (p = P.typeid = = ID). ToList ();
}
return list;
}
[HttpGet]
Public Xitong getone (int id)//query single data
{
Return DB. Xitong.where (T = = T.id = = ID). FirstOrDefault ();
}
[HttpGet]
Public UserInfo Login (string name, string pwd)//Login
{
Return DB. Userinfo.where (T = t.name==name&& t.name==pwd). FirstOrDefault ();
}
public void Post ([Frombody]xitong value)//New data
{
Db. Xitong.add (value);
Db. SaveChanges ();
}
public void Put ([Frombody]xitong value)//Modify
{
var data = db. Xitong.where (T = T.id = = value.id). FirstOrDefault ();
if (data! = NULL)
{
Data. Door = value. Door;
Data. TypeID = value. TypeID;
Data. Moneyinfo = value. Moneyinfo;
Data. Getmoneyman = value. Getmoneyman;
Data. Timeinof = value. timeinof;
}
Db. SaveChanges ();
}
public void Delete (int id)//delete
{
var data = db. Xitong.where (T = = T.id = = ID). FirstOrDefault ();
Db. Xitong.remove (data);
Db. SaveChanges ();
}
}
-----------------------------MVC Section
public class Showcontroller:controller
{
Aentities db = new aentities ();
public static readonly URI address = new Uri ("http://localhost:9556");
Login
[HttpGet]
Public ActionResult Login ()
{
return View ();
}
Public ActionResult Logininfo (string name,string pwd)
{
Uri url = new Uri (address, "/apid/product/login?name=" + name + "&pwd=" + pwd);
using (HttpClient client = new HttpClient ())
{
var result = client. Getasync (URL). Result;
if (result. Issuccessstatuscode)
{
Return Content ("<script>alert (' Login succeeded! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Login failed! ') </script> ");
}
}
}
Show
Public ActionResult Index (string name = "", int id = 0, int pageIndex = 1)
{
var select = db. TypeInfo;
Viewbag.list = new SelectList (SELECT, "ID", "TypeName");
Uri url = new Uri (address, "/apid/product/get?name=" + name+ "&id=" +id);
list<zuohebiao> tt = new list<zuohebiao> ();
using (HttpClient client = new HttpClient ())
{
var result = client. Getasync (URL). Result;
if (result. Issuccessstatuscode)
{
tt = result. Content.readasasync<list<zuohebiao>> (). Result;
}
}
Return View (TT. Topagedlist (pageindex,2));
}
Delete
Public ActionResult Delete (int id)
{
Uri url = new Uri (address, "/apid/product/delete?id=" +id);
using (HttpClient client = new HttpClient ())
{
var result = client. Deleteasync (URL). Result;
if (result. Issuccessstatuscode)
{
Return Content ("<script>alert (' Delete succeeded! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Delete failed! '); location.href= '/show/index ' </script> ");
}
}
}
Add to
[HttpGet]
Public ActionResult ADD ()
{
var select = db. TypeInfo;
Viewbag.list = new SelectList (SELECT, "ID", "TypeName");
return View ();
}
Public ActionResult Add (Xitong str)
{
Uri url = new Uri (address, "/api/product");
using (HttpClient client = new HttpClient ())
{
var result = client. Postasjsonasync (URL,STR). Result;
if (result. Issuccessstatuscode)
{
Return Content ("<script>alert (' Add success! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Add failed! '); location.href= '/show/index ' </script> ");
}
}
}
Modify
[HttpGet]
Public ActionResult Put ()
{
var select = db. TypeInfo;
Viewbag.list = new SelectList (SELECT, "ID", "TypeName");
return View ();
}
Public ActionResult Put (Xitong str)
{
Uri url = new Uri (address, "/apid/product/put");
using (HttpClient client = new HttpClient ())
{
var result = client. Putasjsonasync (URL, str). Result;
if (result. Issuccessstatuscode)
{
Return Content ("<script>alert" modified successfully! '); location.href= '/show/index ' </script> ");
}
Else
{
Return Content ("<script>alert (' Modify failed! '); location.href= '/show/index ' </script> ");
}
}
}
}
--------------Display
------------modification
Route plus action
MVC cross-Domain API HttpClient method EF Dbfirst