0

Goal: Have a partial view in an MVC application that posts and returns to whatever view the partial was rendered in. In this case, the View is Index.html and the PartialView is _ConfigurationSetup.cshtml.

The view for now simply calls the action that renders the partial (I have a model attached):

@{ Html.RenderAction("LoadSiteConfigurationForm", "SiteConfiguration"); }

I use render action so I can pass a model to the Partial View:

public ActionResult LoadSiteConfigurationForm()
{
  SiteConfigurationModel model = new SiteConfigurationModel();
  model = _configRepository.GetConfiguration();
  return PartialView("_ConfigurationSetup", model);
}

In the partial view I have a TinyMCE TextBox, and all I wanted to do is pass a string to the controller, stuff it in the database, and get back without losing the main view (Index.cshtml). I've used Ajax for this very task, and I can't seem to figure out what I'm doing wrong.

Ajax Form (it is contained in a Bootstrap tab, if that's relevant):

    <div id="SetWelcomeMessage" class="tab-pane fade active in">

        @using (Ajax.BeginForm("SetWelcomeMessage", "SiteConfiguration", new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "success",
            HttpMethod = "POST",
        }))
        {
            @Html.LabelFor(m => m.WelcomeMessage)
            @Html.EditorFor(m => m.WelcomeMessage)
            <input type="submit" value="Submit" class="btn btn-default btn-sm margin-top-10" />
        }
    </div>

And finally - the Controller:

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult SetWelcomeMessage(string welcomeMessage)
    {

        _configRepository.SetWelcomeMessage(welcomeMessage);
        return Content("Welcome message has been set!");

    }

I'm certain I am making it to the controller because the database is being updated.

When I return Content I simply see the message on the screen, nothing else. I originally had it returning the partial view, but it only returned the partial view, so I lost everything else.

One thing I did get to work was passing the Index view from the partial, however that doesn't feel right, as they are not in the same place of the project. This will work, it just feels sloppy, and I want to avoid it.

Anything a fresh set of eyes can suggest? Please let me know if you need more information.

Dan Orlovsky
  • 1,045
  • 7
  • 18
  • 1
    Then your missing the `jquery.unobtrusive-ajax.js` file. But better to scrap the awful (and now obsolete) `Ajax.BeginForm()` and use `jQuery.ajax()` –  Mar 16 '16 at 01:17
  • Thank you, I will try that immediately. – Dan Orlovsky Mar 16 '16 at 01:18
  • Thank you, @Stephen - apologies for the duplicate. Sometimes I have a problem articulating my query to provide the best results. Thank you for the heads up on Ajax.BeginForm() as well. – Dan Orlovsky Mar 16 '16 at 01:23

0 Answers0