Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014518
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:30:24+00:00 2026-05-20T09:30:24+00:00

i’m having a hard time dealing with MVC Ajax helper and trying to refresh

  • 0

i’m having a hard time dealing with MVC Ajax helper and trying to refresh certain part of the page. In my code, I have a situation like this:

<div id="ajaxLoadedContent">
<table>
    <tr>
        <th>
            <%: Html.Label("ProductId") %>
        </th>
        <th>
            <%: Html.Label("ProductName") %>
        </th>        
        <th>
        </th>
    </tr>
    <% foreach (var product in Model.Products)
       {
           %>
    <tr>
        <td>
            <%: direccion.ProductId %>
        </td>
        <td>
            <%: direccion.ProductName %>
        </td>

        <td>

            <% using (Ajax.BeginForm("EditProduct", "MyController", 
                    new { ContainerDiv = "ShowEditProducts" }, 
                    new AjaxOptions() { UpdateTargetId = "ShowEditProducts", OnSuccess = "updatePlaceholder" }))
               {%>
            <%: Html.Hidden("ProductId", product.ProductId)%>
            <input type="submit" value="Edit" />
            <%} %>
        </td>
    </tr>
    <% } %>
</table>

<script type="text/javascript">
        function updatePlaceholder(context) {
            var html = context.get_data();
            var placeholder = context.get_updateTarget();
            $(placeholder).html(html);
            return false;
        }
</script>

<div id="ShowEditProducts"></div>
</div>

Now, when I press the Edit button, then the ‘Editor View’ appears, but the problem is that when i Submit that form, then there are two options:

  1. The data is OK, the changes are made, the form dissapears and the list is refreshed.
  2. The data is NOT OK, the forms stays and the validation messages appear.

The problem is that the UpdateTargetId are different for each option.
Option 1 refreshes “ShowEditProducts”, while Option 2 refreshes “ajaxLoadedContent”. Also, ehe ‘Edit View’ contains JavaScript that is executed when the view is loaded using the “Edit” button but is not executed when the form contains errors and is re-rendered.

The code of the EditView looks like this:

<% using (Ajax.BeginForm("SubmitEdit", new AjaxOptions() { UpdateTargetId = Model.ContainerDiv}))
{%>

<script type="text/javascript">
    // My JavaScript                 
</script>

<table>
    <tr>
        <td>
           <%: Html.Label("Id")%></br>
           <%: Html.TextBoxFor(x => x.Product.ProductId)%></br>
        </td>
        <td>
           <%: Html.Label("Name")%></br>
           <%: Html.TextBoxFor(x => x.Product.ProductName)%><
        </td>
    </tr>
</table>

<input type="submit" value="Save" />

<%: Ajax.ActionLink("Cancel", "EmptyView","Shared", new AjaxOptions() { UpdateTargetId = Model.ContainerDiv})%>
<% } %>

So, now my two big problems are:
1. The JavaScript of the ‘Edit View’ don’t get executed when the the view is re-renderd.
2. How can i update one target is there are errors and another one when there are not.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:30:25+00:00Added an answer on May 20, 2026 at 9:30 am

    The object being replaced is being set on this line:

    var placeholder = context.get_updateTarget();
    

    So instead, you should add a condition here. Eg, instead of returning HTML directly, if you return an object that looks like this:

    public class Result
    {
        public bool Success { get; set; }
        public string Html { get; set; }
    }
    

    Then you could do something like this:

    var result = response.get_response().get_object();
    if (result.Success)
        $("#ShowEditProducts").html(html);
    else
        $("#ajaxLoadedContent").html(html);
    

    Alternatively, instead of returning a Success boolean, you could have a property for the ID of the control to replace. This would be more reusable, and might make sense if you want the decision of which control to replace to be done server-side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.