Data to/from a Widget View

Topics: Writing modules
Aug 2, 2011 at 5:58 AM

I'm writing a Contact form as a widget and I'm having trouble getting the data to and from the View.  

The widget is being displayed correctly when assigned to a layer. I can press the 'Send Feedback' ActionLink and that takes me to the Send method in the home controller.

I'm sending my ViewModel as a part in the Driver but I just don't know how to assign the part/model to the input boxes and then pass this back to the controller.

This is what I have so far:

Drivers/ContactUsWidgetDriver.cs

    public class ContactUsWidgetDriver : ContentPartDriver<ContactUsFormViewModel>
    {
        public Localizer T { get; set; }

        public ContactUsWidgetDriver()
        {
            T = NullLocalizer.Instance;
        }


        protected override DriverResult Display(ContactUsFormViewModel part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_ContactUsForm",
                () => shapeHelper.Parts_ContactUsForm(ContentPart: part));

        }
    }

Controllers/HomeController.cs

    public class HomeController : Controller
    {
        private readonly IOrchardServices _services;
        private readonly IContactUsService _contactUsService;
        private readonly ICommentValidator _commentValidator;
        private readonly IMembershipService _membershipService;
        private readonly IMessageManager _messageManager;

        public virtual ISite CurrentSite { get; set; }
        public virtual IUser CurrentUser { get; set; }
        public Localizer T { get; set; }


        public HomeController(IOrchardServices services, IContactUsService contactUsService, ICommentValidator commentValidator, IMessageManager messageManager, IMembershipService membershipService)
        {
            _services = services;
            _contactUsService = contactUsService;
            _commentValidator = commentValidator;
            _membershipService = membershipService;
            _messageManager = messageManager;
        }

        public ActionResult Send()
        {
            var settings = _contactUsService.Get();
            var recipient = _membershipService.GetUser(settings.RecipientsUserName);

            if (recipient == null || String.IsNullOrEmpty(recipient.Email))
            {
                _services.Notifier.Warning(T("Site error: Couldn't send message. Site owner needs to set valid recipient user with an email address."));
                return View();
            
            }

            return Content("Thanks for the feedback.");
        }

Parts/ContactUsForm.cshtml

 

@{
	Style.Include("ContactStyle.css").AtHead();
	Layout.Title = "Contact Form";
}
@using (Html.BeginFormAntiForgeryPost()) {

	<body id="contact-body">
	<div class="inline">
		<h1 id="contact-title">@Layout.Title</h1>
		<span id="contact-subtitle">(*required)</span>
	</div>
		<div id="page-wrap">
			<div id="contact-area">
			
				<form method="post">
					<label id="contact-label" for="Name">Name*:</label>
					<input type="text" name="Name" id="Name" />

					<label id="contact-label" for="Email">Email*:</label>
					<input type="text" name="Email" id="Email" />

					<label id="contact-label" for="Organization">Organization:</label>
					<input type="text" name="Organization" id="Organization" />
				
					<label id="contact-label" for="Message">Message*:</label>
					<textarea name="Message" rows="20" cols="20" id="Message"></textarea>

					<p id="contact-p"> @Html.ActionLink("Send Feedback", "Send", new { Controller = "Home", Area = "TestWidgetProject.ContactUs" }, new { @class = "feedbackButton" })</p>
				</form>		
			<div style="clear: both;"></div>
			</div>
		</div>
	</body>
}

I have used ideas from the Orchard.Jumpstart and Orchard.ContactUs modules but they do not show how to pass unpersisted data to/from a widgets view. Once I have this working I would like to submit it as an upgrade to the EWSNoggin Contact Us project.
Coordinator
Aug 3, 2011 at 1:29 AM

Your Send action looks like it should take parameters so that binders know what to map the submitted form to.