我的数据库没有使用文本框中的新插入值进行更新,但是当加载了“帐户”页面时,使用来自数据库的当前值进行自动完成工作。
我在那里错过了什么?
在Login / SignIn / LogOut表单页面上使用Owin会受到影响吗? 因为Owin在我的数据库中自动生成了少量Tabel,其中一个包含userID和密码。但我不认为这应该是问题。
这是我的UserInfoModel类:
public UserInfo GetUserInformation(string uid)
{
FarmacieEntities db = new FarmacieEntities();
UserInfo info = (from x in db.UserInfoes where x.UID == uid select x).FirstOrDefault();
return info;
}
public void InsertUserInfo(UserInfo info)
{
FarmacieEntities db = new FarmacieEntities();
db.UserInfoes.Add(info);
db.SaveChanges();
}
public string UpdateUserInfo(int id, UserInfo userInfo)
{
try
{
FarmacieEntities db = new FarmacieEntities();
UserInfo p = db.UserInfoes.Find(id);
p.Nume = userInfo.Nume;
p.Prenume = userInfo.Prenume;
p.Adresa = userInfo.Adresa;
p.Telefon = userInfo.Telefon;
p.CodPostal = userInfo.CodPostal;
db.SaveChanges();
return p.Nume + " was succesfully updated";
}
catch (Exception e)
{
return "Error:" + e;
}
}
这是我的网页代码:
protected void Page_Load(object sender, EventArgs e)
{
string id = User.Identity.GetUserId();
Completare(id);
}
private void Completare(string id)
{
UserInfoModel model = new UserInfoModel();
UserInfo user = model.GetUserInformation(id);
txtNume.Text = user.Nume;
txtPrenume.Text = user.Prenume;
txtAdresa.Text = user.Adresa;
txtTelefon.Text = user.Telefon;
txtcod.Text = user.CodPostal;
}
private int getID(string id)
{
UserInfoModel model = new UserInfoModel();
UserInfo user = model.GetUserInformation(id);
return user.ID;
}
private UserInfo CreateUser()
{
UserInfo user = new UserInfo();
user.Nume = txtNume.Text;
user.Prenume = txtPrenume.Text;
user.Adresa = txtAdresa.Text;
user.Telefon = txtTelefon.Text;
user.CodPostal = txtcod.Text;
return user;
}
protected void Button1_Click(object sender, EventArgs e)
{
UserInfoModel model = new UserInfoModel();
UserInfo user = CreateUser();
model.UpdateUserInfo(getID(User.Identity.GetUserId()), user);
Response.Redirect("~/Pages/Account/Comenzi.aspx");
}
答案 0 :(得分:0)
问题已解决: 在页面加载功能中,我忘了在这里添加2行if:if(!IsPostBack)。