Reputation: 25
need some help here..
Aim: Update record on database in MVC4
I have a Accordian view with two buttons #1.Edit #2.save.. Till now what had done is..
using following code i have fetch data from database to corresponding text boxes.
Step 1: Data filled in textboxes are property set as readonly once edit button clicked text boxes will set as editable.
Step 2:if i change data and click save button that should update on database
Problem:
While updating the data in the database updated data is adding in the new row as a new data. But i need the existing data should be updated
View:
<body>
<div id='cssmenu'>
<ul>
<li class='active'></li>
<li class='has-sub'>
<a href='#'><span>Profile</span></a>
<ul>
<form method="post" >
<table>
<tr>
<td>
@Html.DisplayNameFor(model => model.FirstName)
</td>
<td>
@Html.TextBoxFor(m => m.FirstName, new { @readonly = "readonly" })
@*@Html.EditorFor(model => model.FirstName)*@
@Html.ValidationMessageFor(model => model.FirstName)
</td>
</tr>
<tr>
<td>
@Html.DisplayNameFor(model => model.LastName)
</td>
<td>
@Html.TextBoxFor(m => m.LastName, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.LastName)
</td>
</tr>
<tr>
<td>
@Html.DisplayNameFor(model => model.EmailID)
</td>
<td>
@Html.TextBoxFor(m => m.EmailID, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.EmailID)
</td>
</tr>
<tr>
<td>
@Html.DisplayNameFor(model => model.MobileNumber)
</td>
<td>
@Html.TextBoxFor(m => m.MobileNumber, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.MobileNumber)
</td>
</tr>
</table>
<input type="button" id="btn" name="Edit" value="Edit"/>
<input type="submit" name="Save" id="Save" />
@*<input type="submit" name="Save" value="Save" onclick="location.href='@Url.Action("Save", "CU")'" />*@
</form>
</ul>
</li>
<li class='has-sub'>
<a href='#'><span>Change Password</span></a>
<ul>
<li><a href='#'><span>Company</span></a></li>
<li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</li>
<li class='has-sub'>
<a href='#'><span>Add Customer</span></a>
<ul>
<li><a href='#'><span>Company</span></a></li>
<li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</li>
</ul>
</div>
</body>
@section scripts
{
<script type="text/javascript">
$("#btn").click(function () {
$("#FirstName").removeAttr("readonly");
$("#LastName").removeAttr("readonly");
$("#EmailID").removeAttr("readonly");
$("#MobileNumber").removeAttr("readonly");
});
</script>
}
Controller
public ActionResult AccountPanel(int id, string Save, string FirstName)
{
var profile = (from s in db.tblUsers
where s.UserTypeId == 3 && s.UserID == id
select new Profile
{
FirstName = s.FirstName,
LastName = s.LastName,
EmailID = s.EmailID,
MobileNumber = s.MobileNumber
}).FirstOrDefault();
if (Save != null)
{
using (var context = new SYTEntities())
{
var s = context.tblUsers.Where(a => a.UserID == id).FirstOrDefault();
s.FirstName = FirstName;
s.LastName = profile.LastName;
s.EmailID = profile.EmailID;
s.MobileNumber = profile.MobileNumber;
context.tblUsers.Add(s);
context.SaveChanges();
}
}
return View(profile);
}
Upvotes: 1
Views: 284
Reputation:
To update data in the database please change to below code.
if (Save != null)
{
using (var context = new SYTEntities())
{
var s = context.tblUsers.Where(a => a.UserID == id).FirstOrDefault();
s.FirstName = FirstName;
s.LastName = profile.LastName;
s.EmailID = profile.EmailID;
s.MobileNumber = profile.MobileNumber;
context.SaveChanges();
}
}
remove context.tblUsers.Add(s);
from your code
Upvotes: 1
Reputation: 712
using (var context = new SYTEntities())
{
var s = context.tblUsers.Where(a => a.UserID == id).FirstOrDefault();
s.FirstName = FirstName;
s.LastName = profile.LastName;
s.EmailID = profile.EmailID;
s.MobileNumber = profile.MobileNumber;
// SET STATE TO CHANGED
db.Entry(tblUsers).State = EntityState.Modified;
context.SaveChanges();
}
Upvotes: 1