簡體   English   中英

從mysql數據庫更改值行而不是添加

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM