Web_Api_demo1
//登录
public AdminStator Login(string name,string pwd)
{
using(uccontext ctx=new uccontext())
{
returm ctx.AdminStator.where(m=>(m.name==name)&&(m.pwd==pwd)).FirstOrDefault();
}
}
//显示
public list<Commodity>Show()
{
using(uccontext ctx=new uccontext())
{
return Commodity.Include("AdminStator").Tolist();
}
}
//添加
public int Adection(Commodity com)
{
using(uccontext ctx=new uccontext())
{
ctx.commodity.Add(com)
return ctx.savechanges();
}
}
//删除
public int Delete(int id)
{
using(uccontext ctx=new uccontext())
{
Comdity com=ctx.commodity.FirstOrDefault(m=>m.Cid==id)
ctx.Commodity.Remove(com)
return ctx.SaveChanges();
}
}
//修改
public int Update(Commodity com)
{()
using(uccontext ctx=new uccontext())
{
ctx.Commodity.Attach(com);
ctx.Entry(com).State=EntityState.Modified;
return ctx.SaveChanges();
}
}
//反填
public Commodity Revest(int id)
{
using(uccontext ctx=new unccontext())
{
return ctx.Commodity.FirstOrDefault(m=>m.Cid==id);
}
}
//绑定下拉列表
public list<ProType>GetBding()
{
using(uccontext ctx=new uccontext())
{
return ctx.ProType.Tolist();
}
}
//模糊查询
public list<Commodity>Refe(string name="",string id="")
{
using(uccontext ctx=new uccontext())
{
int Rid=string.IsNullOrEmpty(id)?0:int.parse(id);
reture ctx.Commodity.Include("Protype").where
(m=>(string.IsNullOrEmpty(name)?true:m.Cname.Contains(name))
&&(string.IsNullOrEmpty(id)?true:m.pid==Rid)).Tolist();
}
}
//根据id进行显示 比如显示详细信息
public list<Commodity>ShowList(string id)
{
int a =int.parse(id);
using(uccontext ctx=new uccontext())
{
return ctx.Commodity.Where(m=>m.Cid==a).Tolist();
}
}
Web_Mvc_demo2
//封装方法
Private string Postting(string Verbs,string Method,Object obj=null)
{
Task<HttpResponseMessage>task=null;
HttpClient Client=new HttpClient();
URI uri=new URI(“”);
Client.BaseAddress=uri;
Switch(Verbs.Tolower())
{
Case”get”:
Task=Client.GetAsyns(Method);
Break;
Case”post”:
Task=Client.PostAsJsonAsync(Method,Obj);
Break;
Case”put”:
Task=Client.PutAsJsonAsync(Method,Obj);
Break;
Case:”delete”:
Task=Client.DeleteAsync(Method);
Break;
}
Task.Wait();
HttpResponseMessage Response=task.Result;
String json=””;
If(Response.IsSuccessStatuesCode)
{
Var result=Response.Content.ReadAsStringAsync();
Result.Wait();
Json=result.Resul;
}
Return json;
}
//管理员和用户登陆
Public ActionResult Login(string name,string pwd)
{
String n=Request.Form[“name”];
String p=Resqust.Form[“pwd”];
String Cbk=Request[“Checkbox1”];
If(n= =””||p= =””)
{
Response.Write(“<script>alert(‘输入的不能为空’)</script>”)
}
Else
{
If(Cbk==”普通”)
{
String json=Postting(“get”,”Login/”+name+”?pwd”+pwd);
If(json!=null)
{
Session[“Return”]=JsonConvert.DesserializeObject<Adminstator>(json);
Return Redirect(“/Condity/Index”)
}
Else
{
Response.Write(“<script>alert(‘登录失败’)</script>”)
}
}
Else if(Cbk= =”管理”)
{
String json =postting(“get”,”Login2/”+name+”?pwd”+pwd);
If(json!=”null”)
{
Session[“Return”]=JsonConvert.DesserializeObject<Adminstator>(json);
Return Redirect(“/Condity/Index”);
}
Else
{
Response.Write(“<script>alert(‘登录失败’)</script>”);
}
}
}
Return View();
}
//普通登陆
Public ActionResult Login()
{
Return view()
}
[system.web.mvc.httppost]
Public ActionResult Login()
{
String json=postting(“get”,”Login/”+name+”?pwd”+pwd);
Adminstator adm=jsconvert.deserializeobject<adminstator>(json);
If(adm!=null)
{
Session[“adm”]=adm;
Return readirect(“/Admin/index”);
}
Else
{
Response.write(“<script>alert(‘登录失败’)</script>”)
}
Return View();
}
//根据id进行查询
Public Actionresult Particular(int id)
{
String json=postting(“get”,”Particular/”+id);
Commodity list=jsonvert.deserialobject<commodity>(json);
Return view(list)
}
//根据管理员id进行显示
Public ActionResult Index()
{
Int Rid((adminstator)session[“adm”]).Aid;
String json=postting(“get”,”show”/,id);
List<commodity>list=jsonconvery.deserializeobject<list<commodity>>(json);
Return View(list);
}
//添加
Public actionresult adection()
{
Return.view();
}
[system.web.mvc.httppost]
Public actionresult adection(commodity com)
{
String json=postting(“post”,”adection”,com)
If(int.parse(json)>0)
{
Response.write(“<script>alert(‘添加成功’)</script>”);
}
Return View();
}
//上传图片
Public Actionresult Adection()
{
GetBding();
Return.view();
}
[system.web.mvc.httppost]
Public Actionresult Adection(HttpPostedFileBase img,commodity com)
{
If(img!=null)
{
String path=path.combine(request.mapath(“/demo/iamges”),img.FileName);
Img.saveas(path);
Com.img=”/demo/images”+img.FileName;
}
Com.Aid=((adminstator)Session[“returm”]).Aid;
String json=psotting(“post”,”add”,com);
Return int.pares(json);
}
//删除
Public void Delete(int id)
{
String json=postting(“delete”,”Delete/”,id);
If(int.parse(json)>0)
{
Response.write(“<script>alert(‘删除成功’)</script>”)
}
}
//反填
Public ActionResult Update(int id)
{
GetBding();
String json=postting(“get”,”Revest/”+id);
Commodity list=jsonconvert.desrializeObject<commodity>(json);
Return view(list);
}
[system.web.mvc.httppost]
Public actionresult Update(httppostedfilebase img,commodity com)
{
If(img!=null)
{
String path=path.combine(request.mapath(“/demo/iamges”),img.FileName);
Img.SaveAs(path);
Com.Img=”/demo/images”+img.FileName;
}
String json=postting(“put”,”update”,com)
Return int.pares(json)L
}
//上传图片
Public Actionresult Adection()
{
GetBding();
Return view();
}
[system.web.mvc.httppost]
Public Actionresult Adection(commodity com)
{
Com.Aid((adminstator) Session[“adm”]).Aid
If(com.imgfile!=null)
{
String path =path.combine(request.mapath(“/demo/”),com.imgfile.filename);
Com.imgFile.saveas(path);
Com.img=”/demo/”+com.imgfile.filename;
Com.imgfile=null;
}
String json=postting(“get”,”Adection”,com);
Return int.parse(json);
}
//绑定下拉
Public void GetBding()
{
String json =postting(“get”,”GetBding”)
List<protype> list=jsonconvert.deserialiobject<list<protype>>(json);
Var linq =from a in list
Select new selectitem
{
Text=a.name
Value=a.pid.tostring()
};
Viewbag.bding=linq.tolist();
}
//文件上传AJAX方法
@{
Layout = null;
}
@model WebApplication39.Models.Commodity
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Adection</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/demo/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.js"></script>
<script src="~/demo/Content/jquery.form.js"></script>
<script src="~/Scripts/jquery.validate-vsdoc.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<link href="~/demo/bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet" />
<script>
$(function () {
$("#Submit1").click(function () {
if ($("#form0").valid()) {
$("#form0").ajaxSubmit({
type: "post",
url: "/Admin/Adection",
success: function (data) {
if (data > 0) {
alert("上传成功");
location.href = "/Admin/Show";
}
}
})
}
})
$("#Imgfile").change(function () {
var reader = new FileReader();
reader.onload = function (a) {
$("#img1").prop("src", a.target.result);
}
reader.readAsDataURL(this.files[0]);
})
})
</script>
<style>
#img1 {
margin-left:88px;
}
</style>
</head>
<body>
<div style="400px;margin:0 auto">
<h1>欢迎:<span style="color:pink">@(((WebApplication39.Models.Adminstator)Session["adm"]).LoginID) </span>这位大帅哥</h1>
@using (Ajax.BeginForm("Adection", "", new AjaxOptions { }, new { @enctype = "multipart/form-data" }))
{
<table class="table table-bordered">
<tr>
<td>照片名称</td>
<td>@Html.TextBoxFor(m => m.Name)</td>
</tr>
<tr>
<td>请选择类别</td>
<td>
@Html.DropDownListFor(m => m.Pid, ViewBag.Bding as List<SelectListItem>)
</td>
</tr>
<tr>
<td>内容</td>
<td>@Html.TextAreaFor(m => m.Content)</td>
</tr>
<tr>
<td>图片</td>
<td>@Html.TextBoxFor(m => m.Imgfile, new { @type = "file" })</td>
</tr>
<tr>
<td colspan="2"><img src="" alt="" id="img1" width="100" height="130" /></td>
</tr>
<tr>
<td colspan="2" style="text-align:center"><input id="Submit1" type="button" value="添加影集" class="btn btn-link"/></td>
</tr>
</table>
}
</div>
</body>
</html>