[英]Change values row from mysql database instead of adding
我想在用户在表单中编辑用户时更改其属性。
这是我的看法:
@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中的Action方法:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.