thatdude thatdude - 3 months ago 21
ASP.NET (C#) Question

overriding a value on a jquery post to a controller

The basic idea of what I'm trying to accomplish is this:

I have a "save" button and a "remove" button. If the user edits an account and saves, we assume the account is active, and therefore a value of 1 ("True") is preserved. If he/she clicks on the "Remove" button, then they are saving the same data, except that the account is no longer active, therefore we should save a 0 instead ("False"). How can I change the status of the account in case the user decides to go on with the "remove" action? I tried setting .val() for "IsActive" to .val('False') so it could be evaluated and turned into a 0 in Convert.ToBoolean but the application froze.

Here is my jquery post for the remove action:

$("#RemoveAccount").click(function(e){


$('#Display').show();
$('#Display2').show();

$.post("/RxCard/SaveAccount",
{
IsActive: $("#chkIsActive").val('False'),
AccountId: $("#AccountID").val(),
AccountName: $("#txtAccountName").val(),
Address: $("#txtAddress").val(),
City: $("#txtCity").val(),
State: $("#txtState").val(),
ZipCode: $("#txtZip").val(),
Area: $("#txtArea").val(),
Prefix: $("#txtPrefix").val(),
Suffix: $("#txtSuffix").val()


}).done(function (output) {
if (output.length > 0)
alert(output)
}).always(function () {
$("body").css("cursor", "default").delay(1000);
// loadAccount(accountId);
});
});


Here is the controller:

[ValidateRequest]
[HttpPost]
public string SaveAccount(FormCollection form)
{
Pharmacy pharmacy = new Pharmacy();
var isactive = Convert.ToBoolean(form["IsActive"])?1:0;
int AccountID = Convert.ToInt32(form["AccountId"]);
var AccountName = form["AccountName"];
var Address = form["Address"];
var City = form["City"];
var State = form["State"];
var ZipCode = form["ZipCode"];
var PhoneNumber = "(" + form["Area"] + ") " + form["Prefix"] + "-" + form["Suffix"];


using (OdbcConnection _conn = new OdbcConnection("FILEDSN=c:\\datasources\\RxCard.dsn"))
using (OdbcCommand cmd1 = new OdbcCommand())
{
cmd1.Connection = _conn;
cmd1.CommandText = "{call web.Maint_UpdateClinic(?,?,?,?,?,?,?,?,?)}";
cmd1.Parameters.AddWithValue("@AccountID", AccountID);
cmd1.Parameters.AddWithValue("@IsActive", isactive);
cmd1.Parameters.AddWithValue("@AccountName", AccountName);
cmd1.Parameters.AddWithValue("@Address", Address);
cmd1.Parameters.AddWithValue("@City", City);
cmd1.Parameters.AddWithValue("@State", State);
cmd1.Parameters.AddWithValue("@ZipCode", ZipCode);
cmd1.Parameters.AddWithValue("@PhoneNumber", PhoneNumber);
cmd1.Parameters.AddWithValue("@WebID", CookieStore.GetCookie("WebId"));

cmd1.CommandType = CommandType.StoredProcedure;
_conn.Open();
cmd1.ExecuteNonQuery();
_conn.Close();
}
//Response.Redirect("~/rxcard/search");
return string.Empty;
}

Answer

if you have two separate click handlers for "RemoveAccount" and "SaveAccount", why don't you set "IsActive" directly

$("#RemoveAccount").click(function(e){


    $('#Display').show();
    $('#Display2').show();

    $.post("/RxCard/SaveAccount",
 {
     IsActive: false, //for save account IsActive : true
     AccountId: $("#AccountID").val(),
     AccountName: $("#txtAccountName").val(),
     Address: $("#txtAddress").val(),
     City: $("#txtCity").val(),
     State: $("#txtState").val(),
     ZipCode: $("#txtZip").val(),
     Area: $("#txtArea").val(),
     Prefix: $("#txtPrefix").val(),
     Suffix: $("#txtSuffix").val()


 }).done(function (output) {
     if (output.length > 0)
         alert(output)
 }).always(function () {
     $("body").css("cursor", "default").delay(1000);
     // loadAccount(accountId);
 });     

});

Comments