Use various updatetargetid based views

I have a webpage displaying a list of elements in an HTML table. Above the list is a form that allows the user to add a new item to the list through AJAX (using Ajax.BeginForm). When the data is sent to the controller via AJAX, I add a new item to the database database and generate a new table row through the Partial View, which is then added to the existing table.

If the form for adding new elements, however, contains errors, I want to return the form to a web browser and display it.

So here is the question: is it possible to specify UpdateTargetId from the controller? At the moment, any view that I return from the controller is inserted into the same target, but I would like to update another target (i.e. another UpdateTargetId) based on any view returned by the controller.

Any help would be appreciated ...

+4
source share
3 answers

I think a workaround can use json and check the result in the callback function. this is how it will look

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")}) } } 

Today I faced the same problem and could not find a “legitimate” way to do this. I think this simple workaround should do this. the code is currently completely unverified. I am going to realize this tomorrow morning.
Note: Please read this blog post for an alternative solution.
considers

+5
source

I think another idea could be redirected to the server on another controller / action using

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

therefore, if the form contains a RedirectToAction error ... which may even contain another PartialView that may be placed in another DIV ...

just a thought ...

0
source

Basically you want to return 2 different results depending on certain conditions while you make an ajax request. Unfortunately, this is not possible with the actual implementation of AjaxForm. You can only update one region of your form.

A possible workaround for this problem would be to create a partial view with a form for updating and returning a partial view from the controller, after which you will create a new row in the table with plain old javascript in the onSuccess event.

0
source

All Articles