0

I have a form with data from a db:

<div class="overview" style="border-bottom: 1px solid gainsboro;">
    <h2 class="title">CONTACT INFORMATION</h2>
    <div class="left" >

        <div class="input-group" id="Display">
            <span class="store-name" id="lblAccountName">@Model.Pharmacy.AccountName</span>
            <span class="store-address" id="lblAddressBlock">@Model.Pharmacy.Address, <br /> @Model.Pharmacy.City, @Model.Pharmacy.State @Model.Pharmacy.ZipCode</span>
        </div>
        @using (Html.BeginForm("SaveAccount", "RxCard", FormMethod.Post, new { id="Save", enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            <fieldset>
                <div id="Edit">
                    <input id="chkIsActive" style="margin: 5px; " type="hidden" name="chkIsActive"  value="@Model.Pharmacy.IsActive" @(Model.Pharmacy.IsActive == "True" ? "checked=\"checked\"" : "") />
                    <input style="margin: 5px; " type="hidden" name="AccountID" id="AccountID" value="@Model.Pharmacy.AccountID" />
                    <label id="lblAccountName">Account Name</label>
                    @Html.ValidationMessageFor(model => model.Pharmacy.AccountName)
                    @Html.TextBoxFor(model => model.Pharmacy.AccountName, new { @id = "txtAccountName" })
                    <label id="lblAddress">Address</label>
                    @Html.ValidationMessageFor(model => model.Pharmacy.Address)
                    @Html.TextBoxFor(model => model.Pharmacy.Address, new { @id = "txtAddress", @Name = "txtAddress" })
                    <label id="lblCity">City</label>
                    @Html.ValidationMessageFor(model => model.Pharmacy.City)
                    @Html.TextBoxFor(model => model.Pharmacy.City, new { @id = "txtCity", @Name = "txtCity" })
                    <label id="lblState">State</label>
                    @Html.ValidationMessageFor(model => model.Pharmacy.State)
                    @Html.TextBoxFor(model => model.Pharmacy.State, new { @id = "txtState", @Name = "txtState" })
                    <label id="lblZip">Zip</label>
                    @Html.ValidationMessageFor(model => model.Pharmacy.ZipCode)
                    @Html.TextBoxFor(model => model.Pharmacy.ZipCode, new { @id = "txtZip", @Name = "txtZip" })
                    <label id="lblPhoneNumber">Phone Number</label>
                    @Html.TextBoxFor(model => model.Pharmacy.Area, new { @id = "txtArea", @onkeyup = "tabout(this,'txtPrefix');", @maxlength = "3", @style = "float:left; width:4em" })
                    @Html.TextBoxFor(model => model.Pharmacy.Prefix, new { @id = "txtPrefix", @onkeyup = "tabout(this,'txtSuffix');", @maxlength = "3", @style = "float:left; width:4em" })
                    @Html.TextBoxFor(model => model.Pharmacy.Suffix, new { @id = "txtSuffix", @onkeyup = "tabout(this,'txtSuffix');", @maxlength = "4", @style = "float:left; width:4em" })     
                </div>
            </fieldset>
            <br />
            <button type="submit" name="EditAccount" id="EditAccount" value="Edit">Edit</button>
            <button type="button" name="SaveAccount" id="SaveAccount" value="@Model.Pharmacy.AccountID">Save</button>
            <button type="reset" name="Cancel" id="Cancel">Cancel</button>
            <button type="button" name="RemoveAccount" id="RemoveAccount" value="@Model.Pharmacy.AccountID">Remove</button>
        }
    </div>
    <div class="right" id="Display2">
        <a href="tel:@Model.Pharmacy.PhoneNumber" class="phone"><img src="~/images/icon-phone.png" height="25" width="25">@Model.Pharmacy.PhoneNumber</a>
    </div>
    <div class="float-clear"></div>
</div>

and a jquery post:

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

    //e.preventDefault();
    $('#Edit').hide();
    $('#Display').show();
    $('#Display2').show();
    $('#Cancel').hide();
    $('#RemoveAccount').show();
    $('#SaveAccount').hide();
    $('#EditAccount').show();

    $("body").css("cursor", "wait");

    $.post("/RxCard/SaveAccount",
    {
        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);
    });      
});

that passes back any changes made to the form to the "SaveAccount" 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;
}

The save action works just fine except that any changes made don't reflect in "Display":

<div class="input-group" id="Display">
  <span class="store-name" id="lblAccountName">@Model.Pharmacy.AccountName</span>
  <span class="store-address" id="lblAddressBlock">@Model.Pharmacy.Address, <br /> @Model.Pharmacy.City, @Model.Pharmacy.State @Model.Pharmacy.ZipCode</span>
</div>

or in "Display2":

<div class="right" id="Display2">
     <a href="tel:@Model.Pharmacy.PhoneNumber" class="phone"><img src="~/images/icon-phone.png" height="25" width="25">@Model.Pharmacy.PhoneNumber</a>
</div>

which are located above and below the form itself.

OR in the partial view listing the accounts resulting from the search:

@model RxCard.Models.AccountsPage
@{
    ViewBag.Title = "RxCard";
}

<div class="page">
    <div>
        @Html.Partial("_Nav") 
    </div>

    <section class="main-content acct-list">
        <section class="left-col onview">
            <div class="search-box-wrapper">
                <form action="/rxcard/accounts" method="post">
                    @Html.TextBox("keywords", null, new { @class = "searchBox search-input", @placeholder = "Search..." })
                    <button type="submit" class="btn-primary btn-search text-center">SEARCH</button>
                </form>
            </div>

            <div class="search-box-result-count shadowed">@Model.Results.Count Results Found</div>

            <div class="acct-list-results">
                <ul>
                    @foreach (var item in Model.Results.Pharmacies)
                    {
                        <li class="result">
                            <a class="item" id="@("item" + @item.AccountID)" href="@item.AccountID">
                                <span class="acct-name" style="text-transform:uppercase;">@item.AccountName</span>
                                <span class="acct-address" style="text-transform:uppercase;">@item.Address</span>
                                <span class="acct-address" style="text-transform:uppercase;"> @item.City, @item.State @item.ZipCode</span>
                                <span class="acct-phone">@item.PhoneNumber</span>
                            </a>
                        </li>
                    }
                </ul>
            </div>
        </section>
        <div class="col-md-8">
            <section class="right-col">
                <div id="accountDetails" class="acct-details">
                    @Html.Partial("_SearchResult")
                </div>
            </section>
        </div>
   <</section> 
 </div>

The procedure goes like this:
1. I search "clinic"
2. I get back results from accounts labeled as clinics
3. I click on an account
4. I edit the account information.
5. I save any changes
6. "display" and "display2" show updates after I click on the account one more time
7. The search list never displays the updates.

I understand this is a bit too much code to include in a question and that it may seem like I haven't tried to work out a solution... but I really have no idea how to do this. If anyone could provide a couple of lines of code on how this can be done that would really help.

ekad
  • 14,436
  • 26
  • 44
  • 46
thatdude
  • 77
  • 1
  • 2
  • 10

1 Answers1

0

After saving the form (post to /RxCard/SaveAccount), you have to refresh the HTML manually doing an AJAX call, if you don't want to refresh the entire page. For this call, in the controller, you want to return only the corresponding HTML for the part you want to refresh, thus returning a partial view.

This explains it well: https://stackoverflow.com/a/18256629/722778

So for example, to update "Display":

$.post("/RxCard/SaveAccount",
    {
        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);
    $('#Display').Load('@Html.Action("GetAccount", new {id = Model.ID})');
});

And in the controller:

public ActionResult GetAccount(int id)
{    
    // load account from db
    return PartialView("_Account", model);
}

You'll need the corresponding HTML in the "_Account" partial view.

Community
  • 1
  • 1
hernant
  • 341
  • 3
  • 8