views:

40

answers:

2

Hey There

I have a masterpage that render's the following PartialView:

<% Html.RenderPartial("AddPage); %>

AddPage Controller looks like this:

public class PagController : Controller
{
    [HttpGet]
    public PartialViewResult AddPage()
    {
        return PartialView();
    }

    [HttpPost]
    public PartialViewResult AddPage(FormCollection forms)
    {
        //some logic here
        // some view data here

        return PartialView();
    }
}

And the view looks like this:

<% using (Html.BeginForm("AddPage", "Page", FormMethod.Post, new { ID = "PageManager" })){%>
<%= Html.TextBox("txtAddPage") %>
<input type="submit" value="AddPage" />
<%} %>

My issue is, that when i hit submit i get redirect to : http://localhost:1234/Page/AddPage, when instead i just want the partialview to be submitted, and return some basic view data if needed, as well as stay on the same page.

Am i having a blonde moment here? cause i know i have done this before

EDIT - This partial view is rendered in multiple pages, not just one.

A: 

If you want to submit and only reload part of your page, you'll need to use AJAX. The most basic way to do this would be to render the partial view in an UpdatePanel.

Ian Henry
argh, not what i wanted to hear.. i changed my "AddPage" post methods to return "return Redirect(Request.UrlReferrer.ToString());" but this doesn't help if you want to change certain elements on the specific partial view, cause then it simply reloads it...
Dusty Roberts
A: 

Fullpage postback solution

This is a bit tricky since you have to know where to go back. I suggest you change your view and add two additional hidden fields (or one and parse its value - as you wish) and store current controller/action values in it.

You can then use this data in POST action to return a RedirectResult like:

return RedirectToAction("action_from_field", "controller_from_field");

Ajax postback solution

You can always submit your data using Ajax in which case your postback URL can be anything you want. In your case it should be to the current page URL. Edit: And Ajax would be the preferred solution in your particular case.

Robert Koritnik