Creating Forms
Information on creating forms in Umbraco
Last updated
Was this helpful?
Was this helpful?
@using MyFirstForm.Controllers
@model MyFirstForm.Models.ContactFormViewModel
@using (Html.BeginUmbracoForm<ContactFormController>(nameof(ContactFormController.Submit)))
{
<div class="input-group">
<label asp-for="Name"></label>
<input asp-for="Name" />
</div>
<div>
<label asp-for="Email"></label>
<input asp-for="Email" />
</div>
<div>
<label asp-for="Message"></label>
<textarea asp-for="Message"></textarea>
</div>
<br/>
<input type="submit" name="Submit" value="Submit" />
}using Microsoft.AspNetCore.Mvc;
using MyFirstForm.Models;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Web.Website.Controllers;
namespace MyFirstForm.Controllers;
public class ContactFormController : SurfaceController
{
public ContactFormController(
IUmbracoContextAccessor umbracoContextAccessor,
IUmbracoDatabaseFactory databaseFactory,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger profilingLogger,
IPublishedUrlProvider publishedUrlProvider)
: base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{}
[HttpPost]
public IActionResult Submit(ContactFormViewModel model)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
// Work with form data here
return RedirectToCurrentUmbracoPage();
}
}@using MyFirstForm.Models;
@{
Html.RenderPartial("~/Views/Partials/ContactForm.cshtml", new ContactFormViewModel());
}