I have this:
Controller action:
public ActionResult Edit(int id = 0)
{
UserProfile user = db.SelectByID(id);
return View(user);
//if (id == null)
//{
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
//}
//UserProfile userProfile = db.SelectByID(id);
//if (userProfile == null)
//{
// return HttpNotFound();
//}
//return View(userProfile);
}
ModelView:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
And the view:
@model ContosoUniversity.Models.UserProfile
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm("Edit","Account"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Lola Biker</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.UserId)
<div class="form-group">
@Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
It is an asp.net mvc4 application and I want to edit the firstName and LastName of the registered user. I add some extra properties to the Register user. But If I run the application I get this error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 221: {
Line 222:
Line 223: UserProfile user = db.SelectByID(id);
Line 224: return View(user);
Line 225: //if (id == null)
Source File: g:\Mijn Documents\My Web Sites\Lolabikes\C#\ContosoUniversity\Controllers\AccountController.cs Line: 223
I am logged in and will be redirected to the edit page like this:
@if (Request.IsAuthenticated)
{
<text>
Hello, @Html.ActionLink(User.Identity.Name, "Edit", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
</text>
}
else
{
<ul>
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
Thank you for your help
I als tried like this:
public ActionResult Edit(int? id)
{
UserProfile user = db.SelectByID(id);
return View(user);
}
but then I still get id = null
I have my Edit now like this:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UserProfile user = db.SelectByID(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
and view:
@Html.ActionLink(User.Identity.Name, "Edit", "Account", new { userId = 123 }, new { title = "Manage" })
I put breakpoint on this: if (id == null)
and it says: null = null
I have the Edit now like this:
public ActionResult Edit(int? userId) {
//if (userId = null )
//{
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
//}
UserProfile user = db.SelectByID(userId);
// if (user == null)
// {
// return HttpNotFound();
// }
return View(user);
}
but user is null
so If I do it like this , what you suggest:
public ActionResult Edit(int userId )
{
//your code here you get the userId to manipulate.
}
ofcourse I see then empty texboxes(firstName, lastName)
ok, I have it now like this:
@Html.ActionLink(User.Identity.Name, "Edit", "Account", new { Id= Context.User.Identity.Name }, new { title = "Manage" })
and a model UserProfile, like this:
[Table("UserProfile")]
public class UserProfile
{
public int Id { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
//ok
public int MyProperty { get; set; }
}
But my Id is ofcourse a integer, also in the datbase. but this:
Id= Context.User.Identity.Name - Identity.Name - I only see Name - that is a string, how to change that??
because: UserProfile user = db.SelectByID(Id); still user is null????
Thank you