BeginFormSitefinity and BeginForm
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;
//[TypeConverter(typeof(ExpandableObjectConverter))]
//public virtual ISubscribeMailinglist Model
//
// get
//
// if (this.model == null)
//
// this.model = this.InitializeModel();
//
// return this.model;
//
//
public
string
TemplateName
get
return
this
.templateName;
set
this
.templateName = value;
public
string
MailingList
get
return
this
.mailingList;
set
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)
try
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"
;
else
try
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;
manager.SaveChanges();
if
(!mailingList.Subscribers().Contains(subscriber))
manager.Subscribe(subscriber, mailingList.Id);
manager.SaveChanges();
flag =
true
;
else
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
>
else
<
h3
>Iscriviti alla mailinglist</
h3
>
using (Html.BeginForm("SetSubscribeMailinglist", "SubscribeMailinglist"))
if (!string.IsNullOrEmpty(Model.ErrorMsg))
<
div
class
=
"alert alert-danger"
>
@Model.ErrorMsg
</
div
>
<
div
class
=
"form-group"
>
@Html.TextBoxFor(u => u.MailAdress, new @class = "form-control" )
<
button
type
=
"submit"
class
=
"btn btn-default"
>Iscriviti</
button
>
</
div
>
</
div
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
Regards,
Svetoslav Manchev
Telerik
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...