November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
Not sure if I understand the questions correctly, but try to answer those:
1. You can create block controller and create action with HttpPost attribute to post data to. In action method take parameter of model you are posting. It should match form input names then model binding will do stuff needed to populate your model.
I have controller with these action methods:
public override ActionResult Index(ContactBlock currentBlock)
{
// If not an Ajax request after post redirecting to page with status parameter.
var status = Request.QueryString["status"];
// Create view model
var viewModel = new ContactBlockViewModel
{
// Populate current page link which will be used in post method when not an Ajax request to know where to redirect.
PageLink = CurrentPage.ContentLink,
// Property to know if post was successfull. Based on this can generate some message.
Success = !string.IsNullOrEmpty(status) && status == "success",
// Add more properties here
};
var viewModel = PopulateViewModel(currentBlock);
return PartialView("~/Views/Shared/Blocks/_ContactBlock.cshtml", viewModel);
}
// Accepting ContactBlockViewModel same as view model returned previously in Index action, but now it is populated with data.
[HttpPost]
public ActionResult Contact(ContactBlockViewModel model) {
// Get get page URL from where form was posted.
var returnUrl = _urlResolver.GetUrl(model.PageLink);
// Do some stuff with model here
if (Request.IsAjaxRequest())
{
// For Ajax request return partial view with form. Validation will work out of the box.
return PartialView("~/Views/Shared/Blocks/_ContactBlockForm.cshtml", model);
}
// Normal request - redirect to the page. Validation will not work, but can pass anything in query string and then handle on index action.
return Redirect(returnUrl + "?status=success");
}
And views looks like this:
_ContactBlock.cshtml
@model ContactBlockViewModel
<div>
@{ Html.RenderPartial("Blocks/_ContactBlockForm", Model); }
</div>
_ContactBlockForm.cshtml
@model ContactBlockViewModel
<div id="contact-block-form">
@using (Ajax.BeginForm("Contact", "ContactBlock", new AjaxOptions {UpdateTargetId = "contact-block-form", HttpMethod = "Post", InsertionMode = InsertionMode.Replace}))
{
@if (Model.Success)
{
<div class="success">Success!</div>
return;
}
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.PageLink)
<h4>
@Html.LabelFor(m => m.Name)
</h4>
<p>
@Html.TextBoxFor(m => m.Name)
</p>
<p>
<input type="submit" value="Save">
</p>
}
</div>
2. About accessing block data in post action. It is tricky.
One way is to add it to view model and create hidden fields in form and then post the data. The issue with this is that you might not want to send sensitive data to browser.
Other way is to add block's ContentGuid to view model and create hidden field for it. To get block ContentGuid you have to cast currentBlock to IContent, but it is implementing IContent only when used as shared block. Then in post action you can use IContentRepository to get block data by ContentGuid. Disadvantage of this approach is that blocks can be only shared blocks and for local bloks it will not work.
Here are some good articles how others solved posting issue:
http://thisisnothing.nystrom.co.nz/2013/11/19/using-custom-forms-in-an-episerver-mvc-block-template/
http://www.eyecatch.no/blog/2013/01/using-xforms-and-mvc-in-an-episerver-7-block/
Hi,
I've got two questions about navigation inside EPIServer.
First of all, is it possible to access POST parameters inside a block controller (Index action) without using HttpContext.Request... and so on to check every parameters (so crap).
ex : A form posts an ID of an item i'm expecting to watch the detail, in a block. The form targets a page and i'd like to retrieve the ID (post param) inside my block.
2. Inside a block, i'm refreshing a tab with Ajax.BeginForm. The PartialView is returned by an action on the same block controller. Inside this action, i'm trying to access the block datas (especially a ContentReference of a page) which is used in the partialview. Am I able to access the block datas in this action or should I post every block datas I want to retrieve ?
Thanks,
Paqman