Following the Umbraco.tv videos, I've created a contact form to be used via the RTE. The form/surface controller work great, but I want to parameterize some of the properties in my ViewModel. For the life of me, I can't find an example of how to use macro parameters with this setup.
ViewModel
public class ContactFormViewModel { [Required] public string FirstName { get; set; }
[Required] public string LastName { get; set; }
[Required] [EmailAddress] public string Email { get; set; }
public string Phone { get; set; }
[Required] public string Comments { get; set; }
public readonly string EmailFrom { get { return umbraco.UmbracoSettings.NotificationEmailSender; } }
//Would like to set these via macro parameters public int RedirectId { get; set; } public string EmailTo { get; set; } public string EmailSubject { get; set; } }
SurfaceController
public class ContactFormSurfaceController : SurfaceController { public ActionResult Index() { return PartialView("ContactForm", new ContactFormViewModel()); }
[HttpPost] [ValidateAntiForgeryToken] public ActionResult HandleFormSubmit(ContactFormViewModel model) { if (!ModelState.IsValid) return CurrentUmbracoPage();
Ok, I've found an example (https://www.youtube.com/watch?v=eovAHqxoZZU) that's passing the macro parameter value into the model, and then uses hidden fields to post back to the surface controller. Is there any way to retain these values without using hidden fields?
Unfortunately, no, I haven't found a solution to this. I settled on hidden fields for now, but ideally there would be a straight-forward way to pass these parameters encrypted. Hopefully this will be further documented in the future. If I find a solution, I will certainly update this post. Good luck!
Couldn't you do it like this (VB though)
Controller:
Function Index(ByVal questionNumber As Integer, ByVal question As String) As ActionResult
Dim myModel As Object = New CsiEntryViewModel With {.QuestionNumber = questionNumber, .Question = question}
Return PartialView("~/views/Partials/CsiEntryView.vbhtml", myModel)
End Function
In the Macro Partial:
@Html.Action("Index", "CsiEntrySurface", new { questionNumber = @Model.MacroParameters["questionNumber"], question = @Model.MacroParameters["question"]})
@using (Html.BeginUmbracoForm<MyProject.Controllers.ContactFormSurfaceController>("HandleFormSubmit", new { EmailTo = Model.EmailTo, EmailSubject = Model.EmailSubject, RedirectId = Model.RedirectId }, new { @class = "form" }))
Basically, the MacroPartial has access to the macro parameters, so I'm passing them to the partial in the ContactFormViewModel object.
In the partial, I'm using the "AdditionalRouteVals" parameter to pass the values to the surface controller at which point they're available in the model, i.e. model.EmailSubject.
Using Macro Parameters in Surface Controller
Hi All,
Following the Umbraco.tv videos, I've created a contact form to be used via the RTE. The form/surface controller work great, but I want to parameterize some of the properties in my ViewModel. For the life of me, I can't find an example of how to use macro parameters with this setup.
ViewModel
SurfaceController
Partial
MacroPartial
At what point can I grab properties from the macro?
Any insight would be greatly appreciated! :)
Thanks,
Ben
Ok, I've found an example (https://www.youtube.com/watch?v=eovAHqxoZZU) that's passing the macro parameter value into the model, and then uses hidden fields to post back to the surface controller. Is there any way to retain these values without using hidden fields?
Thanks!
Ben
Hello Ben
Did you find a solution for this?
Kind regards
Joel
Hi Joel,
Unfortunately, no, I haven't found a solution to this. I settled on hidden fields for now, but ideally there would be a straight-forward way to pass these parameters encrypted. Hopefully this will be further documented in the future. If I find a solution, I will certainly update this post. Good luck!
Ben
Couldn't you do it like this (VB though) Controller:
In the Macro Partial:
It might be bad practice - I don't know...
This recently came up for me again and I've gotten around the issue with the following changes:
MacroPartial
Partial (modified @using...)
Basically, the MacroPartial has access to the macro parameters, so I'm passing them to the partial in the ContactFormViewModel object.
In the partial, I'm using the "AdditionalRouteVals" parameter to pass the values to the surface controller at which point they're available in the model, i.e. model.EmailSubject.
Hope this helps others! :)
is working on a reply...