Reputation: 1
Basically I'm creating a simple register function and I'm stuck at edit.
This is my model:
public partial class User
{
public int UserId { get; set; }
[Required(ErrorMessage ="This field is required")]
public string User_name { get; set; }
[Required(ErrorMessage = "This field is required")]
public string User_funame { get; set; }
[Required(ErrorMessage = "This field is required")]
[DataType(DataType.Password)]
public string User_pass { get; set; }
[DataType(DataType.Password)]
[DisplayName("Confirm Password")]
[Compare("User_pass")]
public string confirmpass { get; set; }
[Required(ErrorMessage = "This field is required")]
public string User_no { get; set; }
[Required(ErrorMessage = "This field is required")]
public string User_age { get; set; }
[Required(ErrorMessage = "This field is required")]
public string User_email { get; set; }
}
And this is my controller for editing:
public class UserController:Controller
{
public ActionResult TestingUserProfile()
{
int id = Convert.ToInt32(Session["userID"]);
using (DBModels dc = new DBModels())
{
if (id == 0)
{
return RedirectToAction("Login");
}
return View(dc.Users.Find(id));
}
}
public ActionResult Edit(int id)
{
using (DBModels db = new DBModels())
{
return View(db.Users.Where(x => x.UserId == id).FirstOrDefault());
}
}
[HttpPost]
public ActionResult Edit(int id, User user)
{
try
{
using (DBModels db = new DBModels())
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("TestingUserProfile");
}
catch
{
return View();
}
}
}
And this is my view. So instead of showing the whole list of users, it will only shows the current logged in user's details.
@model WebApplication1.Models.User
@{
ViewBag.Title = "User Profile";
}
<h2>User Profile</h2>
<div>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayName("User name :")
</dt>
<dd>
@Html.DisplayFor(model => model.User_name)
</dd>
<br />
<dt>
@Html.DisplayName("Full name :")
</dt>
<dd>
@Html.DisplayFor(model => model.User_funame)
</dd>
<br />
<dt>
@Html.DisplayName("Password :")
</dt>
<dd>
@Html.DisplayFor(model => model.User_pass)
</dd>
<br />
<dt>
@Html.DisplayName("Phone Number :")
</dt>
<dd>
@Html.DisplayFor(model => model.User_no)
</dd>
<br />
<dt>
@Html.DisplayName("Age :")
</dt>
<dd>
@Html.DisplayFor(model => model.User_age)
</dd>
<br />
<dt>
@Html.DisplayName("Email :")
</dt>
<dd>
@Html.DisplayFor(model => model.User_email)
</dd>
<br />
</dl>
</div>
<p>
@Html.ActionLink("Edit", "EditUserProfile", new { id = Model.UserId }) |
@Html.ActionLink("Back", "SelectCourse")
</p>
And this is the view for editing.
@model WebApplication1.Models.User
@{
ViewBag.Title = "EditUserProfile";
}
<h2>Edit User Profile</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.UserId)
<div class="form-group">
@Html.LabelFor(model => model.User_name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User_funame, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_funame, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_funame, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User_pass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_pass, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_pass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User_no, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_no, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_no, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User_age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_age, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User_email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User_email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User_email, "", new { @class = "text-danger" })
</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", "TestingUserProfile")
So when i try to edit the current logged in user This is how it looks. no error message pop up and it just refresh the page every time I click Save. It just return View() instead of saving into the database. i dont know where is the problem. Please help Thanks !
[HttpPost]
public ActionResult Edit(int id, User user)
{
try
{
using (DBModels db = new DBModels())
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("TestingUserProfile");
}
catch
{
return View();
}
}
Upvotes: 0
Views: 725
Reputation: 11
I Think that your View Edit action link should be like below
@Html.ActionLink("EditUserProfile","Edit", new { id = Model.UserId })
or
<a href="@Url.Action("Edit")/@Model.UserId" >EditUserProfile</a>
Upvotes: 1