NathanB
NathanB

Reputation: 23

HTTP Error 404.0 when editing User in ASP.Net - MVC

I'm pretty new to programming in C#, ASP.NET and the MVC framework so I'm still trying to figure everything out with some help from e-books and of course the internet.

I'm making a test web application to see if I understand some of the basics of programming in ASP.NET - MVC and I started with the default MVC template that you get when you start a new project in Visual Studio. With this in mind you should know that this template already has a working User login and register function and after I connected this to my database I could register as much user as I want and login with these user. To take it a little bit further I want to edit the user properties after the account is registered. After I addes the code below, I can see my user information and I can edit them, but when i press the Save button I get the following error message:

http://puu.sh/pl5L5/b70da8b40d.png

Please keep in mind that I'm a newbie so my apologizes if this question is rather stupid. Thanks in advance!

THE CODE

A new view under the ManageController:

@using (Html.BeginForm("EditUser", "Manage", FormMethod.Post))
{
    <h4>Edit User Form.</h4>
    @Html.AntiForgeryToken()

    foreach (var prop in ViewData.ModelMetadata.Properties)
    {
        <div class="form-group">
            <label>@(prop.DisplayName ?? prop.PropertyName)</label>
            @if (prop.PropertyName == "Description")
            {
                @Html.TextArea(prop.PropertyName, null, new { @class = "form-control", rows = 5 })
            }
            else
            {
                @Html.TextBox(prop.PropertyName, null, new { @class = "form-control" })
            }
            @Html.ValidationMessage(prop.PropertyName)
        </div>
    }
    <div class="panel-footer">
        <input type="submit" value="Opslaan" class="btn btn-primary" />
        @Html.ActionLink("Cancel and return to List", "Index", null, new { @class = "btn btn-default" })
    </div>
}

A new EditUserViewModel:

public class EditUserViewModel
{
    public string Id { get; set; }

    [Required(AllowEmptyStrings = false)]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }

    [Required(AllowEmptyStrings = false)]
    [Display(Name = "Voornaam")] //FirstName
    public string Voornaam { get; set; }

    [Required(AllowEmptyStrings = false)]
    [Display(Name = "Achternaam")] //LastName
    public string Achternaam { get; set; }

    public string Telefoonnummer { get; set; } //Phonenumber
}

And 2 new methods in the ManageController:

    public async Task<ActionResult> EditUser(string id)
    {
        id = this.User.Identity.GetUserId();
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var user = await UserManager.FindByIdAsync(id);
        if (user == null)
        {
            return HttpNotFound();
        }

        var userRoles = await UserManager.GetRolesAsync(user.Id);

        return View(new EditUserViewModel()
        {
            Email = user.Email,
            Voornaam = user.Voornaam,
            Achternaam = user.Achternaam,
            Telefoonnummer = user.PhoneNumber
        });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> EditUser([Bind(Include = "Id, Email, Voornaam, Achternaam, Telefoonnummer")]EditUserViewModel editUser)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByIdAsync(editUser.Id);
            if (user == null)
            {
                return HttpNotFound();
            }

            user.UserName = editUser.Email;
            user.Email = editUser.Email;
            user.Voornaam = editUser.Voornaam;
            user.Achternaam = editUser.Achternaam;
            user.VolledigeNaam = editUser.Email;
            user.PhoneNumber = editUser.Telefoonnummer;

            await UserManager.UpdateAsync(user);

            return RedirectToAction("Index");
        }
        ModelState.AddModelError("", "Something failed.");
        return View();
    }

Upvotes: 1

Views: 101

Answers (1)

NathanB
NathanB

Reputation: 23

After searching and searching and searching I finally found the problem. In the ManageController in the EditUser method is had following code:

    return View(new EditUserViewModel()
    {
        Email = user.Email,
        Voornaam = user.Voornaam,
        Achternaam = user.Achternaam,
        Telefoonnummer = user.PhoneNumber
    })

But the problem was that the ID field didn't get the value of the user. I forgot this because I didn't want the users to be able to edit their ID. So the code should be:

    return View(new EditUserViewModel()
    {
        Id = user.Id
        Email = user.Email,
        Voornaam = user.Voornaam,
        Achternaam = user.Achternaam,
        Telefoonnummer = user.PhoneNumber
    })

Upvotes: 0

Related Questions