Having issue posting on contact form

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 年 前
I have copy pasted form from the contact us page and created  a partial view. Here is the code:

  public class ContactformNoProductViewComponent : NopViewComponent
    {
        private readonly ICommonModelFactory _commonModelFactory;
        private readonly CaptchaSettings _captchaSettings;
        private readonly CommonSettings _commonSettings;
        private readonly ICustomerActivityService _customerActivityService;
        private readonly IHtmlFormatter _htmlFormatter;
        private readonly ILanguageService _languageService;
        private readonly ILocalizationService _localizationService;
        private readonly IWorkContext _workContext;
        private readonly IWorkflowMessageService _workflowMessageService;

        public ContactformNoProductViewComponent(CaptchaSettings captchaSettings,
            CommonSettings commonSettings,
            ICommonModelFactory commonModelFactory,
            ICustomerActivityService customerActivityService,
            IHtmlFormatter htmlFormatter,
            ILanguageService languageService,
            ILocalizationService localizationService,
            SitemapSettings sitemapSettings,
            SitemapXmlSettings sitemapXmlSettings,
            IWorkContext workContext,
            IWorkflowMessageService workflowMessageService
            )
        {
            _captchaSettings = captchaSettings;
            _commonSettings = commonSettings;
            _commonModelFactory = commonModelFactory;
            _customerActivityService = customerActivityService;
            _htmlFormatter = htmlFormatter;
            _languageService = languageService;
            _localizationService = localizationService;
            _workContext = workContext;
            _workflowMessageService = workflowMessageService;

        }

        public async Task<IViewComponentResult> InvokeAsync()
        {
            var model = new Nop.Web.Models.Common.ContactUsModel();
            model = await _commonModelFactory.PrepareContactUsModelAsync(model, false);

            return View(model);

        }

        [HttpPost, ActionName("ContactUsC")]
        [ValidateCaptcha]
        //available even when a store is closed
        [CheckAccessClosedStore(true)]
        public virtual async Task<IViewComponentResult> ContactUsC(ContactUsModel model, bool captchaValid)
        {
            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage && !captchaValid)
            {
                ModelState.AddModelError("", await _localizationService.GetResourceAsync("Common.WrongCaptchaMessage"));
            }

            model = await _commonModelFactory.PrepareContactUsModelAsync(model, true);

            if (ModelState.IsValid)
            {
                var subject = _commonSettings.SubjectFieldOnContactUsForm ? model.Subject : null;
                var body = _htmlFormatter.FormatText(model.Enquiry, false, true, false, false, false, false);

                await _workflowMessageService.SendContactUsMessageAsync((await _workContext.GetWorkingLanguageAsync()).Id,
                    model.Email.Trim(), model.FullName, subject, body);

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

                //activity log
                await _customerActivityService.InsertActivityAsync("PublicStore.ContactUs",
                    await _localizationService.GetResourceAsync("ActivityLog.PublicStore.ContactUs"));

                return View(model);
            }

            return View(model);
        }
    }


Here is the  cshtml file;

@using Nop.Web.Models.Common


@model Nop.Web.Models.Common.ContactUsModel
@{
    NopHtml.AppendPageCssClassParts("html-contact-page");

}


<div id="contact-us-tab" class="contact-page write-review">
    @if (Model.SuccessfullySent)
        {
            <div class="result">
                @Model.Result
            </div>
        }
        else
        {
            <form asp-controller="ContactformNoProduct" asp-action="ContactUsC" id="ContactUsForm" name="ContactUsForm" method="post">
                <div asp-validation-summary="ModelOnly" class="message-error"></div>
                <div class="fieldset">
                    <div class="form-fields">
                        <div class="inputs">
                            <label asp-for="FullName" asp-postfix=":"></label>
                            <input asp-for="FullName" placeholder="@T("ContactUs.FullName.Hint")" class="fullname" />
                            <nop-required />
                            <span asp-validation-for="FullName"></span>
                        </div>
                        <div class="inputs">
                            <label asp-for="Email" asp-postfix=":"></label>
                            <input asp-for="Email" placeholder="@T("ContactUs.Email.Hint")" class="email" />
                            <nop-required />
                            <span asp-validation-for="Email"></span>
                        </div>
                        @if (Model.SubjectEnabled)
                        {
                            <div class="inputs">
                                <label asp-for="Subject" asp-postfix=":"></label>
                                <input asp-for="Subject" placeholder="@T("ContactUs.Subject.Hint")" class="subject" />
                                <nop-required />
                                <span asp-validation-for="Subject"></span>
                            </div>
                        }
                        <div class="inputs">
                            <label asp-for="Enquiry" asp-postfix=":"></label>
                            <textarea asp-for="Enquiry" placeholder="@T("ContactUs.Enquiry.Hint")" class="enquiry"></textarea>
                            <nop-required />
                            <span asp-validation-for="Enquiry"></span>
                        </div>
                        @if (Model.DisplayCaptcha)
                        {
                            <nop-captcha />
                        }
                    </div>
                </div>
                <div class="buttons">
                    <button type="submit" name="send-email" class="button-1 contact-us-button">@T("ContactUs.Button")</button>
                </div>
            </form>
        }
</div>






This works :InvokeAsync  however I cannot get the post to work how can i post it to the same file as listed above and page should not redirect etc.

Thank you
1 年 前
Check both the browser Console, and System > Log for any error messages (just after you click the 'sublut' button)
1 年 前
It never posts . Goes to https://localhost:5001/page-not-found!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.