On my blog I have an example of modelbinding a form model:
http://krompaco.nu/2015/01/working-with-your-own-forms-in-episerver-and-mvc/
You have the option to use any controller among Page, Block or DefaultPage I guess.
If you need to keep track of which Block was posting you could construct some kind of identifier, such as CurrentPage.PageLink.ID + "-" + CurrentBlock.ContentLink.ID. If you want to support same block on same page twice you need to add something more (ItemIndex or something). Use the identifier in a hidden element in your block's <form>, for example <input type="hidden" name="formidentifier" value="@FormIdentifier" />. Make sure your FormModels contain a prop named "FormIdentifier".
Off the top of my head... :-)
EDIT: You should use x-@FormIdentifier in your ID's and <label for="x-@FormIdentifier"> I forgot to mention.
Hi Johan and thanks for help!
I was also thinking to use some kind of block / form identifier in order to solve the "label for" issue.
But in that case, how can I make model binders to work with form identifiers?
I’d like to use ModelState.AddModelError to highlight invalid fields and display validation summary.
I guess I have to write controller actions in page controller, not in block controllers, because on each post-back I have to render the entire page + info from ModelState, right?
Is there something else I need to include in the route / hidden fields?
For example, if I use the following code:
<form action="MyAction" method="Post">
This works fine if I:
- go to start page
- Click on the link that takes me to domain.com/my-page
- submit the form and use return View("Index", myModel); in controller action
- asp.net will redirect me to domain.com/my-page/MyAction, which is fine
But if I open a new instance of browser, navigate directly to domain.com/my-page and submit the form, this time asp.net will redirect me to domain.com/MyAction (notice that my-page part is missing), which returns 404.
I found this article: http://epiwiki.se/developing/mvc/sample-how-to-use-post-backs-in-episerver-mvc
Haven’t tested it yet :)
Hi,
I have to allow the editor to drag & drop forms to an XHTML field.
These are custom forms, not xforms.
I can create a block for each form, and then use:
This works fine… but there’s a catch :)
The site should work when javascript is disabled.
Some of the challenges I have:
Is there any sample with custom forms inside page blocks with some basic validation?
Thanks!