Search code examples
jqueryajaxasp.net-mvcpartial-views

MVC reload Parent View from Partial View


I am developing an MVC Application. I have a Parent View that contains a Partial View.

Partial View has a submit function that calls a Controller Method.

If anything goes wrong I need to retrieve Error Message. If everything goes OK, I want to reload Parent View.

In the begining I had a

@using (Html.BeginForm("Create", "Controller", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "javascript:return validate()" }))

in Partial View. So when everything goes OK, I can call a Method to reload Parent View

return RedirectToAction("Create", new { id = Id });

but I can not retrieve an Error Message.

So in order to retrieve an Error Message, and stay in the same View. I have to use Json.

So I use

<form action="@Url.Action("Create", "Controller")" id="form" enctype="multipart/form-data" method="post">

and define an

$('form').submit(function (event)

to do the call to the Controller. But I can not Redirect to another Method to reload Parent View.

My parent View have a model like

@model long

that is the ID of the current record.

When I redirect to another method. the Method do this...

 public ActionResult Create(Id =0)
        {
            return View(Id);
        }

If I do window.location.href =ParentView .. I do not have the ID generated.

the only thing I can do is call another Controller Method and set

$('#div).html(result)

But I need to set a DIV in my Parent View. I do not want to do that. I am looking another way to do it.

What it the correct way to reach boths things?

thanks


Solution

  • If you are looking to pass id value using window.location.href you can do as below:

     window.location.href = '../Controller/Create?id=' + 1;
    

    Here 1 is the id passed to param id in Create action.