0
votes

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

1
use $.ajax method...to pass data to controller in JsonResult or ActionResult and that method return json data and u check that data in success method...Dhiren Patel
Agree with @DhirenPatel. Then you determine how to proceed after the ajax returns. Most often the parent page refresh is a javascript window.location.href = ...nurdyguy
yes, but I want to reload Parent View on Controller Method. I only returns json if an error occurs. I am trying to do a Redirect to another Method if everything is ok..Diego
When i reload Parent View, I need to pass a parameter that is the ID of the current record... how can i do that using window.location.href= ?Diego
for passing parameter use can make ViewModel in javascript and pass to controller and use that link that can help you stackoverflow.com/questions/40260758/…Dhiren Patel

1 Answers

1
votes

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.