logo
Welcome to our new AbleCommerce forums. As a guest, you may view the information here. To post to this forum, you must have a registered account with us, either as a new user evaluating AbleCommerce or an existing user of the application. For all questions related to the older version of Gold and earlier, please go to forums.ablecommerce.com. Please use your AbleCommerce username and password to Login. New Registrations are disabled.

Notification

Icon
Error

Options
Go to last post Go to first unread
Jay  
#1 Posted : Friday, August 5, 2022 4:26:26 PM(UTC)
Jay

Rank: Member

Groups: Authorized User, Developers
Joined: 11/12/2018(UTC)
Posts: 16

Thanks: 1 times
Was thanked: 3 time(s) in 2 post(s)
As I've mentioned in previous posts, I'm an MVC newbie, so I hope this makes sense.

I have a widget (partial view) that is working fine. Among other things, it has a button with type = button that changes the css display property of a table via an inline script, and a button with type = submit for calling the controller's HttpPost method. The controller code runs correctly when the submit button is clicked. I do not want it to leave the current page after submitting, and it doesn't. It uses a custom model I have created. The controller checks some things and if everything is OK, it writes a record to the database. I have the controller's post method returning the partial view, since that what the AbleCommerce widgets I looked at do if they don't go to another page:
Code:
        [HttpPost]
        public ActionResult MyWidgetDialog(MyModel model)
        {
            ...
            return PartialView("_MyWidgetDialog", model);
        }

If everything is OK and the database is successfully updated, I'd like to re-display or reload the partial view after changing the css display property of a couple of html divs. However, based on testing and Google searches, it doesn't seem like returning the partial view like this actually lets you send data back to the partial view, or at least not in a way that can change what is being displayed. Doing some searches led me to try changing the controller method to just return a string in a ContentResult, then changing the submit button to a regular button and using a jQuery script to call that method (passing the model and receiving the string). I haven't been able to get that to work yet. It looks like I would need to convert the model data to a JSON string to do that - does that sound right? Plus, I don't think the model's data validation is done using a jQuery post instead of the normal submit.

Can anyone give me some help on how to accomplish this?

Wanna join the discussion?! Login to your AbleCommerce Forums forum account. New Registrations are disabled.

Joe Payne2  
#2 Posted : Saturday, August 6, 2022 11:05:15 AM(UTC)
Joe Payne2

Rank: Advanced Member

Groups: HelpDesk, Developers
Joined: 11/9/2018(UTC)
Posts: 534

Thanks: 110 times
Was thanked: 21 time(s) in 20 post(s)
You have a few options:

Use an Ajax.BeginForm() and set the target to a div tag that contains the content that needs refreshed. The button becomes just a submit button inside the ajax form. The action result called by the beginform() will return the same partial view and MVC will replace the contents of the specified div tag with the new partial view response html.

Or....

If the button must sit outside the form, make it a regular button with a click event to a javascript method. In the javascript, you make an ajax call to the actionresult which returns the partial view. In the Success: of the ajax call, you populate the div tag with the ajax response.
shaharyar  
#3 Posted : Monday, August 8, 2022 5:15:22 AM(UTC)
shaharyar

Rank: Advanced Member

Groups: Admin, Developers, Registered, HelpDesk, Authorized User
Joined: 10/5/2018(UTC)
Posts: 633

Thanks: 4 times
Was thanked: 108 time(s) in 107 post(s)
Quote:
I'd like to re-display or reload the partial view after changing the CSS display property of a couple of HTML divs.


If you want to preset different display properties when returning from the POST method, you can create a property in the Custom Model class and then update the view HTML based on that property to set CSS display properties.

I would also like to suggest Ajax beginForm to be used.
Jay  
#4 Posted : Monday, August 8, 2022 2:17:14 PM(UTC)
Jay

Rank: Member

Groups: Authorized User, Developers
Joined: 11/12/2018(UTC)
Posts: 16

Thanks: 1 times
Was thanked: 3 time(s) in 2 post(s)
Thanks for the suggestions. The view does already use Ajax.BeginForm. Here's the relevant razor code:
Code:
@model MyModel

<div id="myWidget">
    <div class="login-form-wrapper">
        @using (Ajax.BeginForm("MyDialog", new AjaxOptions() { UpdateTargetId = "myWidget", InsertionMode = InsertionMode.ReplaceWith, HttpMethod = "POST" }))
        {
            @Html.AntiForgeryToken()
            <h3>Dialog Title Text</h3>
            <div id="myFormDiv" class="dialogSection">
                @* more code here for entering/displaying model fields, validation summary, etc. *@
            </div>
            <div id="submittedDiv" class="dialogSection inputForm" style=@ViewData["submittedDivDisplay"]>
                <p>Your request has been submitted.  We will send you an email when your account is ready.</p>
            </div>
        }
    </div>
</div>

Using the VS debugger, I can see that when I click the Submit button my controller code for posting is executing all the way trough, including the "return PartialView(...);" statement, without error:
Code:
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HandleAndLogError(typeof(HttpAntiForgeryException), Logger.LogMessageType.Debug)]
[HttpPost]
public ActionResult MyDialog(MyModel model)
{
    ViewData["submittedDivDisplay"] = "display:none";
    if (ModelState.IsValid)
    {
        // various other code
        model.Save();
        ViewData["submittedDivDisplay"] = "display:block";
    }
    return PartialView("_MyDialog", model);
}

However, in the browser, the http POST request is getting a "400 bad request" response.
If I use a model property instead of ViewData to pass the string, I get the same thing. Nothing shows up in the AC error log or the Windows event log.
I'm not sure how to figure out what is causing the "bad request" response.
nadeem  
#5 Posted : Wednesday, August 10, 2022 9:05:03 AM(UTC)
nadeem

Rank: Advanced Member

Groups: Admin, Administrators, Developers, Registered, HelpDesk, System, Authorized User
Joined: 10/11/2018(UTC)
Posts: 64

Thanks: 8 times
Was thanked: 6 time(s) in 6 post(s)
I have tested your code and it is working as expected i.e. it shows the confirmation message on submit success. I am wondering if you have some error in your saving logic. Is the record saved in the database successfully?
Jay  
#6 Posted : Wednesday, August 10, 2022 10:57:23 AM(UTC)
Jay

Rank: Member

Groups: Authorized User, Developers
Joined: 11/12/2018(UTC)
Posts: 16

Thanks: 1 times
Was thanked: 3 time(s) in 2 post(s)
Thanks for testing Nadeem. Yes, the model record is saved correctly to the database. I guess I need to do some more debugging. I guess I should start with a very simple view and controller code, and if that works, then start adding the details back in until it stops working.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.