I'm working with MVC 4 web app conecct to a SQL DB on my server.
I have a page(for admin to control users managment) with a list(table) of users, each row represent a user with the fields :
- User Name
- Date Created
- Role
- Status
And each row have 3 button in right side :
- Deactivate
- Edit(Role)
- Delete User
A user can be active & unactive (freeze user account temporarily) using the button "Deactivate".
When you press the "Deactivate" button the user status changes to "unactive" and the button value(text) is changes to "Active", using Jquery.
Now i want to be able to call a function(server side) when the" Activate/Deactivate" button is clicked in order to update the DB, but i dont want to call a function on the controller and return the same view and force the page to post back, i want to update the DB only without any changes in the presentation layer(changing view and etc) because i take care of the presentation layer with help from Jquery.
How Can I Do It?
The code:
A row in my table:
<tr>
<td width="15%">@appUser.name</td>
<td class="text-left" width="15%">@appUser.date</td>
<td class="text-left" width="10%">@appUser.role</td>
<td id="user_email" class="text-left" width="24%">@appUser.email</td>
@if(appUser.activeStatus)
{
<td class="text-left" width="10%"><span id=@Url.Content("status_lbl" + index) class="label label-success">Active</span></td>
<td class="btn-group-sm text-center ">
<input id="@Url.Content("freeze_btn" + index)" value="Deactivate" class="btn btn-default" onclick="@Url.Action("SetActive", "Home", new { email = appUser.email, activeStatus = false }) " />
}
else
{
<td class="text-left" width="10%"><span id=@Url.Content("status_lbl" + index) class="label label-danger">Unactive</span></td>
<td class="btn-group-sm text-center ">
<input id="@Url.Content("freeze_btn" + index)" value="Activate" class="btn btn-default" onclick="@Url.Action("SetActive", "Home", new { email = appUser.email, activeStatus = false }) " />
}
<input type="button" value="Edit" class="btn btn-default " />
<input type="button" value="Delete" class="btn btn-default " />
</td>
</tr>
Jquery: (Taking care of the presentation)
$('input[id^=freeze_btn]').click(function (event) {
var btn = $(event.target);
var text = $(this).attr('value');
var newid = $(this).attr('id').replace('freeze_btn', 'status_lbl');
if (text == "activate ") {
btn.val("Deactivate");
$('#' + newid).removeClass("label-danger").addClass("label-success").text("Active");
$(this).attr('onclick').replace('true', 'false');
}
if (text == "Deactivate") {
btn.val("activate ");
$('#' + newid).removeClass("label-success").addClass("label-danger").text("Unactive");
$(this).attr('onclick').replace('false', 'true');
}
});
How can i call a function(code behind) with the the paramters
- User Email(in order to search the user in my DB
- The value(text) of the button "Active/Deactivate" (so i will know if to activate the user or to Deactivate without accessing the DB in order to check the user current status because i do it once the page load (not implemented yet))
And stay on the view, no return view just a function working in bacgground updating the database nothing more.(This is very important!)