BeginFormSitefinity and BeginForm

Posted by Community Admin on 05-Aug-2018 14:45

BeginFormSitefinity and BeginForm

All Replies

Posted by Community Admin on 14-May-2015 00:00

Dear sirs,

we are building a custom widget to allow mailing list Subscription using (Feather MVC widget)

If we use BeginFormSitefinity (in the view) the action in the html does not evaluate to the correct path and is not routed to the widget controller

If we use BeginForm everything run fines.

Could You please elaborate about that behavior?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.Mvc;
using easyandpet.web.sitefinity.widgets.Mvc.Models;
using Telerik.Sitefinity.Frontend.Mvc.Helpers;
//using Telerik.Sitefinity.Frontend.Identity.Mvc.StringResources;
using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers;
using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers.Attributes;
//using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Mvc;
//using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Modules.Newsletters;
using Telerik.Sitefinity.Newsletters.Model;
using Telerik.Sitefinity.Web;
namespace easyandpet.web.sitefinity.widgets.Mvc.Controllers
    [Telerik.Sitefinity.Mvc.ControllerToolboxItem(Name = "SubscribeMailinglist", SectionName = "E&P Widgets", Title = "Subscribe Mailinglist", CssClass = "")]
    public class SubscribeMailinglistController : Controller
        private string templateName = "SubscribeMailinglist";
        private string templateNamePrefix = "SubscribeMailinglist.";
        private string mailingList = "Generale";
        //private SubscribeMailinglistViewModel model;
        //private ISubscribeMailinglist model;
        //public virtual ISubscribeMailinglist Model
        //      get
        //              if (this.model == null)
        //                      this.model = this.InitializeModel();
        //              return this.model;
    public string TemplateName
            return this.templateName;
            this.templateName = value;
        public string MailingList
                return this.mailingList;
                this.mailingList = value;
    public ActionResult Index(bool MailAccettata = false, string error = null)
        SubscribeMailinglistViewModel viewModel = new SubscribeMailinglistViewModel();
        viewModel.ErrorMsg = error;
        viewModel.MailOK = MailAccettata;
        string str = string.Concat(this.templateNamePrefix, this.TemplateName);
        return base.View(str, viewModel);
        //private ISubscribeMailinglist InitializeModel()
        //      return ControllerModelFactory.GetModel<ISubscribeMailinglist>(base.GetType(), null);
        public ActionResult SetSubscribeMailinglist(SubscribeMailinglistInputModel model)
        bool flag = false;
        string empty = string.Empty;
                if (!base.ModelState.IsValid)
                        ModelState modelState = base.ModelState.Values.FirstOrDefault<ModelState>((ModelState v) => v.Errors.Any<ModelError>());
                        if (modelState != null)
                            ModelError modelError = modelState.Errors.FirstOrDefault<ModelError>();
                            if (modelError != null)
                                empty = modelError.ErrorMessage;
                    catch (Exception ex)
                        empty = "errore validazione";
                            NewslettersManager manager = NewslettersManager.GetManager();
                            MailingList mailingList = manager.GetMailingLists().Where(l => l.Title.Equals(this.MailingList, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                            if (mailingList != null)
                                Subscriber subscriber = manager.GetSubscribers().Where(s => s.Email.Equals(model.MailAddress, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                                Guid subscriberId;
                                if (subscriber == null)
                                    subscriberId = new Guid();
                                    subscriber = manager.CreateSubscriber(true, subscriberId);
                                    subscriber.FirstName = "";
                                    subscriber.LastName = "";
                                    subscriber.Email = model.MailAddress;
                                if (!mailingList.Subscribers().Contains(subscriber))
                                    manager.Subscribe(subscriber, mailingList.Id);
                                    flag = true;
                                    empty = string.Format("l'indirizzo email 0 è gia associato alla mailinglist", model.MailAddress);
            catch (Exception exception)
                empty = "errore di iscrizione mail";
        string pageUrl = string.Empty;
                Guid? pageId;
                PageSiteNode actualCurrentNode = SiteMapBase.GetActualCurrentNode();
                if (actualCurrentNode == null)
                    return null;
                pageId = new Guid?(actualCurrentNode.Id);
        empty = HttpUtility.UrlEncode(empty);
                string str = HyperLinkHelpers.GetFullPageUrl(pageId.Value);
                string str1 = string.Format("?MailAccettata=0&error=1", flag, empty);
        pageUrl = string.Concat(str, str1);
        return this.Redirect(pageUrl);



@model easyandpet.web.sitefinity.widgets.Mvc.Models.SubscribeMailinglistViewModel
@using Telerik.Sitefinity.UI.MVC;
@using Telerik.Sitefinity.Frontend.Mvc.Helpers
<div class="@Model.CssClass">
    @if (Model.MailOK)
        <h3>Mail inserita nella lista</h3>
        <h3>Iscriviti alla mailinglist</h3>
            using (Html.BeginForm("SetSubscribeMailinglist", "SubscribeMailinglist"))
            if (!string.IsNullOrEmpty(Model.ErrorMsg))
                <div class="alert alert-danger">
            <div class="form-group">
               @Html.TextBoxFor(u => u.MailAdress, new @class = "form-control" )
                   <button type="submit" class="btn btn-default">Iscriviti</button>

Posted by Community Admin on 19-May-2015 00:00

Hi Francesco,

Would you please try to use the conversion structure of the widgets as described in the following articles:
Create custom models, controllers, and views
Create a “Hello World” MVC widget in a separate assembly

Svetoslav Manchev

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items

Posted by Community Admin on 19-May-2015 00:00

Hi Svetoslav,

the widget is working fine and to build that I already followed the examples/instructions from the link you posted.

The problem is only related to BeginFormSitefinity and BeginForm

When I use BeginFormSitefinity in the view, the postback is not routed to the widget controller...

This thread is closed