MVC+API根据ID进行传值

//Sql语句(API)

public int Login(string username,string userpwd)
{

List<UserInfo> list = dal.Login(username, userpwd);
if (list.Count > 0)
{
return Convert.ToInt32(list[0].Id);
}
else
{
return 0;
}

}

 

//MVC

public ActionResult Index(int id)
{
ViewBag.id = id;

return View();
}

 

function Login(){
var obj = {
username: $("#UserName").val(),
userpwd: $("#UserPwd").val()
}
$.ajax({
url: 'http://localhost:14369/api/Default/Login',
data: obj,
type: "get",
dataType: "json",
success: function (data) {
if (data != 0) {
alert("登录成功");

location.href = '/Default/Index/' + parseInt(data);
}
else {
alert("登录失败");

}
}
})
}


public ActionResult Login()
{

return View();
}

 

function load() {
var uid =@ViewBag.id;
@*var uid = @Session["A"]*@
$.ajax({
url: 'http://localhost:14369/api/Default/GetShopCard?uid=' + uid,
type: 'get',
//data: {uid:uid},
dataType: 'json',
success: function (data) {

$("#td").empty();
$(data).each(function () {
$("#tb").append('<tr>'
+ '<td><input name="cbOne" type="checkbox"/></td>'
+ '<td>'
+ ' <table>'
+ ' <tr>'
+ ' <td>' + this.Name + '</td>'
+ ' </tr>'
+ ' <tr>'
+ ' <td><img src="' + this.Img + '" alt="" width="80" height="80"/></td>'
+ ' <td>' + this.SName + '</td>'
+ ' </tr>'
+ ' </table>'
+ '</td>'
+ '<td>' + this.Prices + '</td>'
+ '<td><input class="w40" type="button" value="-" onclick="cal("+",' + this.Id + ')" />' + '<input type="text" value="' + this.Num + '"/>' + '<input class="w40" type="button" value="+" onclick="cal("-",' + this.Id + ')" /></td>'
+ '<td><span id="sumPrices">' + this.SumPrices + '</span></td>'
+ '<td><input id="btn_Del" type="button" value="删除" onclick="Del(' + this.Id + ')" /></td>'
+ '</tr>')
})
}
})
}
load();

 

 

 

// linq语句

public object Login(string uname, string upwd)
{
var list = (from u in db.UserInfo where u.UserName == uname && u.UserPwd ==
upwd select u).FirstOrDefault();
return list;
}

[HttpGet]
public object GetShopCard(int uid)
{
var list = (from sc in db.ShopCard
join u in db.UserInfo on sc.UserInfoId equals u.Id
join s in db.Shop on sc.ShopId equals s.Id
join st in db.Store on sc.StoreId equals st.Id
where sc.UserInfoId == uid
select new
{
SCId = sc.Id,
StName = st.Name,
SName = s.SName,
SPrices = s.Prices,
Num = sc.Num,
SumPricec = sc.SumPrices,
img = s.Img
}).ToList();
return new { ShopCards = list };
}

posted @ 2020-08-02 19:50  影子少年  阅读(235)  评论(0)    收藏  举报