gpt4 book ai didi

c# - 更改 mysql 数据库中的值行而不是添加

转载 作者:行者123 更新时间:2023-11-29 08:29:43 25 4
gpt4 key购买 nike

我想在用户在表单中编辑属性时更改用户的属性。

这是我的观点:

@using (Html.BeginForm("Manage", "Account")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()

<fieldset>
<legend>Change Password Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email, new { @Value = ViewBag.Email })
</li>
<li>
@Html.LabelFor(m => m.Adress)
@Html.TextBoxFor(m => m.Adress, new { @Value = ViewBag.Adress })
</li>
<li>
@Html.LabelFor(m => m.Description)
@Html.TextBoxFor(m => m.Description, new { @Value = ViewBag.Description })
</li>
<li>
@Html.LabelFor(m => m.Skills)
@Html.TextBoxFor(m => m.Skills, new { @Value = ViewBag.Skills })
</li>
</ol>
<input type="submit" value="Change password" />
</fieldset>

}

如您所见,如果值已存在于数据库中,我会用该值填充文本框。
当他们单击“提交”时,我想将更改保存在我的存储库中。

这是我的 Controller 中的操作方法:

public ActionResult Manage(ChangeSettingsModel model)
{
if (ModelState.IsValid)
{
try
{
string username = User.Identity.Name;

// Get user
users user = userrepo.FindByUsername(username);

long userid = user.user_id;

userrepo.ChangeSettings(userid, model.Name, model.SurName, model.Email, model.Adress, model.Description, model.Skills, model.Website);
}
catch (ArgumentException ae)
{
ModelState.AddModelError("", ae.Message);
}
}

return View(model);

}

在我的存储库中:

public void ChangeSettings(long userid, string name, string surname, string email, string adress, string description, string skills, string website)
{
//users user = entities.users.SingleOrDefault(u => u.user_id.Equals(userid));

try
{

}

catch (ArgumentException ae)
{
throw ae;
}
catch (Exception)
{
throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
"If the problem persists, please contact your system administrator.");
}

Save();
}

通常我会在尝试中添加一些内容,但现在我不想添加用户而是更改属性。我怎样才能做到这一点?

最佳答案

像下面这样的东西会有帮助。

var query = (from u in entities.users
where u.user_id== UID
select u).First();

query.Address = u.Address;
query.Company = u.Company;
query.Fax = u.Fax;
query.Mobile = u.Mobile;
query.Name = u.Name;
query.Telephone = u.Telephone;
entites.SubmitChanges();

关于c# - 更改 mysql 数据库中的值行而不是添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17008690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com