<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sangeet Shah</title>
    <description>The latest articles on DEV Community by Sangeet Shah (@sangeetshah).</description>
    <link>https://dev.to/sangeetshah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F333282%2Fdbed7cb3-0076-4f29-9668-3cb34343dcdf.jpg</url>
      <title>DEV Community: Sangeet Shah</title>
      <link>https://dev.to/sangeetshah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sangeetshah"/>
    <language>en</language>
    <item>
      <title>Override action method using plugin into nopCommerce3.8</title>
      <dc:creator>Sangeet Shah</dc:creator>
      <pubDate>Tue, 11 Feb 2020 05:52:14 +0000</pubDate>
      <link>https://dev.to/sangeetshah/override-action-method-using-plugin-into-nopcommerce3-8-22b8</link>
      <guid>https://dev.to/sangeetshah/override-action-method-using-plugin-into-nopcommerce3-8-22b8</guid>
      <description>&lt;p&gt;This article learn you how to override the action method in nopcommerce 3.8 version. In this article i am going to overrride the contact us form to extend its funcationliy. but this action method override way is not worked over seo url(route value) like product url,category url etc. &lt;/p&gt;

&lt;p&gt;Now i am going to show how to override the contact us page using plugin. This contact us form having only field like name,emailid,content but i want to add one more field that is contact no that help me to get contact detail of person who going to post this form.&lt;/p&gt;

&lt;p&gt;For this feature i am going to add one project into plugin folder and add the ContactusController.cs, RouteProvider.cs,ContactUsModel.cs, ContactUs.cshtml, app.config,description.txt,OverrideActionMethodProvider.cs, packages.config, web.config. for now i have refer the Nop.Plugin.ExternalAuth.Facebook plugin.&lt;/p&gt;

&lt;p&gt;My plugin structure is as describe with this article.&lt;/p&gt;

&lt;p&gt;I have created&lt;br&gt;
ContactSscontroller.cs =&amp;gt;  for contact us form get or post method.&lt;br&gt;
RouteProvider.cs =&amp;gt; used to over ride the route url of contact us. if user call the contact us url then our plugin form will be display instead of default nopcommerce form(action method). &lt;br&gt;
ContactUsmodel.cs =&amp;gt; to add one more property of contact no.&lt;br&gt;
ContactUs.cshtml =&amp;gt; This view file to display content of contact us form&lt;br&gt;
OverrideActionMethodProvider.cs =&amp;gt; having method of install and uninstall process for plugin.&lt;/p&gt;

&lt;p&gt;ContactUsmodel.cs model file. I have added my new property into this model. I have used the existing ContactUsModel of nopcommerce.&lt;br&gt;
using Nop.Web.Framework;  &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace Nop.Plugin.OverrideActionMethod.Models  
{  
    public class ContactUsModel : Nop.Web.Models.Common.ContactUsModel  
    {  
        [NopResourceDisplayName("Account.Fields.Phone")]          
        public string PhoneNo { get; set; }  
    }  
}  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;RouteProvider.cs routeProvider file override the existing route value &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Nop.Web.Framework.Localization;  
using Nop.Web.Framework.Mvc.Routes;  
using System.Web.Routing;  
using System.Linq;  

namespace Nop.Web.Infrastructure  
{  
    public partial class RouteProvider : IRouteProvider  
    {  
        public void RegisterRoutes(RouteCollection routes)  
        {  
            routes.Remove(routes.FirstOrDefault(c =&amp;gt; (c as Route).Url == "contactus"));  
            routes.MapLocalizedRoute("ContactUs",  
                             "contactus",  
                             new { controller = "ContactUs", action = "ContactUs" },  
                             new[] { "Nop.Plugin.OverrideActionMethod.Controllers" });  
        }    
    }  
}   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I have remove the existing route value of contact us and add my contact us route value. I have mention my controller name, action methon name and namespace for this. &lt;/p&gt;

&lt;p&gt;Contactus.cshtml view file  &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@model ContactUsModel  
@using Nop.Plugin.OverrideActionMethod.Models  
@using Nop.Web.Framework  
@using Nop.Web.Framework.UI  
@using Nop.Web.Framework.Security.Captcha   
@{  
    Layout = "~/Views/Shared/_ColumnsOne.cshtml";  

    //title  
    Html.AddTitleParts(T("PageTitle.ContactUs").Text);  
    //page class  
    Html.AppendPageCssClassParts("html-contact-page");  
}  
&amp;lt;div class="page contact-page"&amp;gt;  
    &amp;lt;div class="page-title"&amp;gt;  
        &amp;lt;h1&amp;gt;@T("PageTitle.ContactUs")&amp;lt;/h1&amp;gt;  
    &amp;lt;/div&amp;gt;  
    &amp;lt;div class="page-body"&amp;gt;  
        @Html.Action("TopicBlock", "Topic", new { systemName = "ContactUs" })  
        @Html.Widget("contactus_top")  
        @if (Model.SuccessfullySent)  
        {  
            &amp;lt;div class="result"&amp;gt;  
                @Model.Result  
            &amp;lt;/div&amp;gt;  
        }  
        else  
        {  
            using (Html.BeginForm("ContactUsSend", "ContactUs", FormMethod.Post, new { id = "form" }))  
            {  
                @Html.AntiForgeryToken()  
                var validationSummary = Html.ValidationSummary(true);  
                if (!MvcHtmlString.IsNullOrEmpty(validationSummary))  
                {  
                    &amp;lt;div class="message-error"&amp;gt;@validationSummary&amp;lt;/div&amp;gt;  
                }  
                &amp;lt;div class="fieldset"&amp;gt;  
                    &amp;lt;div class="form-fields"&amp;gt;  
                        &amp;lt;div class="inputs"&amp;gt;  
                            @Html.LabelFor(model =&amp;gt; model.FullName)  
                            @Html.TextBoxFor(model =&amp;gt; model.FullName, new { @class = "fullname", placeholder = T("ContactUs.FullName.Hint") })  
                            @Html.RequiredHint()  
                            @Html.ValidationMessageFor(model =&amp;gt; model.FullName)  
                        &amp;lt;/div&amp;gt;  
                        &amp;lt;div class="inputs"&amp;gt;  
                            @Html.LabelFor(model =&amp;gt; model.Email)  
                            @Html.TextBoxFor(model =&amp;gt; model.Email, new { @class = "email", placeholder = T("ContactUs.Email.Hint") })  
                            @Html.RequiredHint()  
                            @Html.ValidationMessageFor(model =&amp;gt; model.Email)  
                        &amp;lt;/div&amp;gt;  
                        @if (Model.SubjectEnabled)  
                        {  
                            &amp;lt;div class="inputs"&amp;gt;  
                                @Html.LabelFor(model =&amp;gt; model.Subject)  
                                @Html.TextBoxFor(model =&amp;gt; model.Subject, new { @class = "subject", placeholder = T("ContactUs.Subject.Hint") })  
                                @Html.RequiredHint()  
                                @Html.ValidationMessageFor(model =&amp;gt; model.Subject)  
                            &amp;lt;/div&amp;gt;  
                        }  
                        &amp;lt;div class="inputs"&amp;gt;  
                            @Html.LabelFor(model =&amp;gt; model.Enquiry)  
                            @Html.TextAreaFor(model =&amp;gt; model.Enquiry, new { @class = "enquiry", placeholder = T("ContactUs.Enquiry.Hint") })  
                            @Html.RequiredHint()  
                            @Html.ValidationMessageFor(model =&amp;gt; model.Enquiry)  
                        &amp;lt;/div&amp;gt;  
                        @if (Model.DisplayCaptcha)  
                        {  
                            &amp;lt;div class="captcha-box"&amp;gt;  
                                @Html.Raw(Html.GenerateCaptcha())  
                            &amp;lt;/div&amp;gt;  
                        }  

                        &amp;lt;div class="inputs"&amp;gt;  
                            @Html.LabelFor(model =&amp;gt; model.PhoneNo)  
                            @Html.TextBoxFor(model =&amp;gt; model.PhoneNo)  
                            @Html.ValidationMessageFor(model =&amp;gt; model.PhoneNo)  
                        &amp;lt;/div&amp;gt;  
                    &amp;lt;/div&amp;gt;  
                &amp;lt;/div&amp;gt;  
                &amp;lt;div class="buttons"&amp;gt;  
                    &amp;lt;input type="submit" name="send-email" class="button-1 contact-us-button" value="@T("ContactUs.Button")" /&amp;gt;  
                &amp;lt;/div&amp;gt;  
            }  
        }  
        @Html.Widget("contactus_bottom")  
    &amp;lt;/div&amp;gt;  
&amp;lt;/div&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I have only add phone no text into this view file and change the form post action method name to post into my controller&lt;/p&gt;

&lt;p&gt;ContactUsController.cs &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Nop.Core;  
using Nop.Core.Domain.Common;  
using Nop.Core.Domain.Messages;  
using Nop.Plugin.OverrideActionMethod.Models;  
using Nop.Services.Customers;  
using Nop.Services.Localization;  
using Nop.Services.Logging;  
using Nop.Services.Messages;  
using Nop.Web.Controllers;  
using Nop.Web.Framework.Security;  
using Nop.Web.Framework.Security.Captcha;  
using System;  
using System.Linq;  
using System.Web.Mvc;  


namespace Nop.Plugin.OverrideActionMethod.Controllers  
{  
    public class ContactUsController : BasePublicController  
    {  
        #region Fields  

        private readonly ILocalizationService _localizationService;  
        private readonly IWorkContext _workContext;  
        private readonly IStoreContext _storeContext;  
        private readonly IQueuedEmailService _queuedEmailService;  
        private readonly IEmailAccountService _emailAccountService;  
        private readonly ICustomerActivityService _customerActivityService;  
        private readonly EmailAccountSettings _emailAccountSettings;  
        private readonly CommonSettings _commonSettings;  
        private readonly CaptchaSettings _captchaSettings;  

        #endregion  

        #region Constructors  

        public ContactUsController(  
            ILocalizationService localizationService,  
            IWorkContext workContext,  
            IStoreContext storeContext,  
            IQueuedEmailService queuedEmailService,  
            IEmailAccountService emailAccountService,  
            ICustomerActivityService customerActivityService,  
            EmailAccountSettings emailAccountSettings,  
            CommonSettings commonSettings,  
            CaptchaSettings captchaSettings)  
        {  
            this._localizationService = localizationService;  
            this._workContext = workContext;  
            this._storeContext = storeContext;  
            this._queuedEmailService = queuedEmailService;  
            this._emailAccountService = emailAccountService;  
            this._customerActivityService = customerActivityService;  
            this._emailAccountSettings = emailAccountSettings;  
            this._commonSettings = commonSettings;  
            this._captchaSettings = captchaSettings;  
        }  

        #endregion  

        #region Methods  

        public ActionResult ContactUs()  
        {  
            var model = new ContactUsModel  
            {  
                Email = _workContext.CurrentCustomer.Email,  
                FullName = _workContext.CurrentCustomer.GetFullName(),  
                SubjectEnabled = _commonSettings.SubjectFieldOnContactUsForm,  
                DisplayCaptcha = _captchaSettings.Enabled &amp;amp;&amp;amp; _captchaSettings.ShowOnContactUsPage  
            };  
            return View("~/Plugins/Plugin.OverrideActionMethod/Views/ContactUs/ContactUs.cshtml", model);  
        }  

        [HttpPost]  
        [PublicAntiForgery]  
        [CaptchaValidator]  
        public ActionResult ContactUsSend(ContactUsModel model, bool captchaValid)  
        {  
            //validate CAPTCHA  
            if (_captchaSettings.Enabled &amp;amp;&amp;amp; _captchaSettings.ShowOnContactUsPage &amp;amp;&amp;amp; !captchaValid)  
            {  
                ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));  
            }  

            if (ModelState.IsValid)  
            {  
                var phoneNo = model.PhoneNo;  
                string email = model.Email.Trim();  
                string fullName = model.FullName;  
                string subject = _commonSettings.SubjectFieldOnContactUsForm ?  
                    model.Subject :  
                    string.Format(_localizationService.GetResource("ContactUs.EmailSubject"), _storeContext.CurrentStore.GetLocalized(x =&amp;gt; x.Name));  

                var emailAccount = _emailAccountService.GetEmailAccountById(_emailAccountSettings.DefaultEmailAccountId);  
                if (emailAccount == null)  
                    emailAccount = _emailAccountService.GetAllEmailAccounts().FirstOrDefault();  
                if (emailAccount == null)  
                    throw new Exception("No email account could be loaded");  

                string from;  
                string fromName;  
                string body = Core.Html.HtmlHelper.FormatText(model.Enquiry, false, true, false, false, false, false);  
                //required for some SMTP servers  
                if (_commonSettings.UseSystemEmailForContactUsForm)  
                {  
                    from = emailAccount.Email;  
                    fromName = emailAccount.DisplayName;  
                    body = string.Format("&amp;lt;strong&amp;gt;From&amp;lt;/strong&amp;gt;: {0} - {1}&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;{2}",  
                        Server.HtmlEncode(fullName),  
                        Server.HtmlEncode(email), body);  
                }  
                else  
                {  
                    from = email;  
                    fromName = fullName;  
                }  
                _queuedEmailService.InsertQueuedEmail(new QueuedEmail  
                {  
                    From = from,  
                    FromName = fromName,  
                    To = emailAccount.Email,  
                    ToName = emailAccount.DisplayName,  
                    ReplyTo = email,  
                    ReplyToName = fullName,  
                    Priority = QueuedEmailPriority.High,  
                    Subject = subject,  
                    Body = body,  
                    CreatedOnUtc = DateTime.UtcNow,  
                    EmailAccountId = emailAccount.Id,  
                });  

                model.SuccessfullySent = true;  
                model.Result = _localizationService.GetResource("ContactUs.YourEnquiryHasBeenSent");  

                //activity log  
                _customerActivityService.InsertActivity("PublicStore.ContactUs", _localizationService.GetResource("ActivityLog.PublicStore.ContactUs"));  

                return View("~/Plugins/Plugin.OverrideActionMethod/Views/ContactUs/ContactUs.cshtml", model);  
            }  

            model.DisplayCaptcha = _captchaSettings.Enabled &amp;amp;&amp;amp; _captchaSettings.ShowOnContactUsPage;  
            return View("~/Plugins/Plugin.OverrideActionMethod/Views/ContactUs/ContactUs.cshtml", model);  
        }  
        #endregion  
    }  
}  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This controller help me to display my contact us form and get the form detail into my end to get new property value. &lt;/p&gt;

&lt;p&gt;I hope you got the way to override the action method using plugin in nopcommerce3.8. I have attached the source code of this plugin so you can able to get better idea for this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mediafire.com/file/ze6ck5u2wsho0qk/Nop.Plugin.OverrideActionMethod.zip/file"&gt;https://www.mediafire.com/file/ze6ck5u2wsho0qk/Nop.Plugin.OverrideActionMethod.zip/file&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nopcommerce</category>
      <category>nopcommerceplugin</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
