4

I have a web page which display a list of items in a HTML table. Above the list is a form which allows the user to add a new item to the list via AJAX (using Ajax.BeginForm). When the data is posted to the controller via AJAX I add the new item to the database backend and generate a new table row via a Partial View which then gets appended to the exisiting table.

When the form to add new items however contains errors, I want to render the form back to the web browser and display that.

So here is the question: Is it possible to specify the UpdateTargetId from within the controller? At the moment whatever View I return from the controller gets inserted in the same target, but I would like to update a different target (ie. different UpdateTargetId) based on whatever view was returned from the controller.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jerrie Pelser
  • 538
  • 1
  • 8
  • 15
  • Why would you want to do that? Isn't that violating the separation of concern issue and introduce more complexity to your controller? It is probably easier to seperate them out into different controller actions to simplify and refactor the similar code into its own method. – Johannes Setiabudi May 29 '09 at 15:48
  • Don't see how you can split it out. This is the process: User enters data in form. The Form gets submitted via AJAX. If the form contains errors it must be rendered back to the browser displaying the errors. If it does not contain errors, an new row needs to be added to the table. That is two distinct different views which *might* be rendered back to the browser, and each of those two views each needs to go into different placeholders. – Jerrie Pelser May 29 '09 at 18:11

3 Answers3

5

I think a workaround could be using JSON and checking the result in callback function. This is how it would look like:

function handleAjax(response) {

            var result = response.get_response().get_object();
            var isSuccess = result[0].isSuccess;
            if(isSuccess = "true")
            {
               $("#resultdiv").html(result[0].html);
            }
            else
            {
                $("#formdiv").html(result[0].html);
            } 

        }

In your action you can write something like:

public ActionResult SaveRecord(Entity entity)
{
   if(ModelState.IsValid(){
   return Json(new{isSuccess = true, html = PartialView("RowViewName")}); 
   }
   else
  {
     return json (new{isSuccess = false, html = PartialView("FormViewName")})
  }
}

I just ran into the same problem today and could not find "legal" way of doing this. I think this simple workaround should get it done. At the moment code is completely untested.

Please read this blog post for alternative solution.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • 1
    PartialView("RowViewName") does not return a string as i expected while posting this answer. you have to use a method that can execute the ViewResult and give back a string. check http://stackoverflow.com/questions/483091/render-a-view-as-a-string for rendering a view to string – Muhammad Adeel Zahid Mar 12 '11 at 14:43
0

I think another idea can be redirecting on the server to another controller/action with

RedirectToAction(new {controller="home", action="index", id=931,variable="abc"});

so if the form contains an error RedirectToAction ... which even could contain another PartialView which could be placed in another DIV...

just a thought...

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
silverfighter
  • 6,762
  • 10
  • 46
  • 73
  • Unfortunatelly that won't ve userful since the region to update in the view will still be the same. What Jerrie needs is to change the updatable region depending on the result which is not possible as far as I know. – Gerardo Contijoch Jun 01 '09 at 00:38
  • we essentially need a flag on client side to determine which div we have to update. one way of doing this is json. however my above answer would not serve the purpose because PartialView() does not return html string as i had expected. u can write ur own function that converts partialView to Html String and return it in Jsonresult alongwith a success flag. – Muhammad Adeel Zahid Aug 10 '10 at 16:31
0

Basically what you want is to return 2 different results depending on certain conditions while you're making an ajax request. Unfortunately that's not possible with the actual implementation of AjaxForm. You can update only a single region of your form.

A possible workaround to this problem may be creating a partial view with the form to update and return the partial view from the controller, then you create the new row in the table with plain old javascript on the onSuccess event.

Gerardo Contijoch
  • 2,421
  • 5
  • 20
  • 29