I have a partial view with a form for getting data for insert/update a database record. The page with this partial view is displayed with non problem. Filling-in the data and posting the form works okay (the record is saved correctly into the database). The problem is that although I return a partial view (return PartialView();
) the system replaces the target div with the whole page (and not only with the partial view)!!
From the above symptoms, you can understand that:
- The responding controller is the correct one (the record is saved in the DB).
- The necessary js scripts are not missing (indeed ajax replaces a portion of the page)
Please note that the Partial View starts with Layout = ""
(so it doesn't reference any layout). I have tested also with Layout = null
, with the same results.
Other details:
- Visual Studio Community 2015
- The container page is implemented through Umbraco CMS.
Partial View:
@using FLATS;
@model TMSCounterpartyModel
@{
Layout = "";
TMSCounterpartyModel myModel = (TMSCounterpartyModel)Model;
Html.EnableClientValidation(true);
Html.EnableUnobtrusiveJavaScript(true);
AjaxOptions ajaxOpt = new AjaxOptions {
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "frmEditCounterparty"
};
}
@using(Ajax.BeginForm("HandlePost", "EditTMSCounterparty", null, ajaxOpt, new { @id="editCptyForm" }))
{
@Html.AntiForgeryToken()
<div class="row">
<fieldset>
@Html.HiddenFor(model => model.Id)
...
<div class="row margin-top-20">
<div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-3 col-md-8 col-md-offset-2">
<input id="btnSubmit" type="submit" value="Save" class="form-control" />
</div>
</div>
</fieldset>
</div>
}
Controller:
public class EditTMSCounterpartyController : System.Web.Mvc.Controller
{
[HttpPost]
[ValidateAntiForgeryToken()]
public PartialViewResult HandlePost(TMSCounterpartyModel model)
{
string viewPath = Library.PartialViewFullPath("EditCounterparty.cshtml");
if (ModelState.IsValid == false)
{
return PartialView(viewPath, model);
}
if (model.Save() == true)
{
TMSCounterpartyModel newModel = new TMSCounterpartyModel();
newModel.LoadOrDefault(model.Id);
return PartialView(viewPath, newModel);
}
else
{
return PartialView(viewPath, model);
}
}
}
Any help is appreciated.
Thank you in advance.