0

I want to change the properties of a user when they edit them in a form.

This is my view:

@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>

}

As you can see I fill the text boxes with the value if it's already in the database.
When they click on "Submit" I want to save the changes in my repository.

This is my Action method in my 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);

}

In my repository:

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();
    }

Normally I add something in try but now I don't want to add a user but change the properties. How can I do this?

1 Answer 1

2

something like below would help.

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.