4

I have been struggling from last 2 days to find clear and exemplified documentation on how to build modules using MVC framework for DOTNETNUKE CMS.

It is very unfortunate for a CMS of that size to first advertise app developers that they support MVC, and then have next to negligible information on how to build say a very simple form to begin with.

This is what I have achieved by now. I am trying to build a basic contact us form:

  1. Set up Controller Actions

    // GET: FormEntry Index .. 
    [ModuleAction(ControlKey = "Add", TitleKey = "AddItem")]
    public ActionResult Index()
    {
        return View();
    }
    
    
    
    [HttpPost] // POST: 
    [ActionName("FormEntry")]
    
    public string Post(FormEntry formEntry)
    {
        try
        {
            if (ModelState.IsValid)
            {
                FormEntryManager.Instance.CreateItem(formEntry);
            }
            return "success";
        }
        catch(Exception ex)
        {
            return "error";
        }
    
    }
    
  2. Set up a View.

    <div id="Item-@Dnn.ModuleContext.ModuleId">
    
        <form id="formcontactus">
            @*@Html.ValidationSummary(true)*@
    
            <fieldset>
                <div class="dnnFormItem">
                    <div><label for="itemName">@Dnn.LocalizeString("lblName")</label></div>
                    @Html.TextBoxFor(m => m.VisitorName)
                    @Html.ValidationMessageFor(m => m.VisitorName, @Dnn.LocalizeString("VisitorNameRequired"))
                </div>
                <div class="dnnFormItem">
                    <div><label for="itemDescription">@Dnn.LocalizeString("lblVisitorEmail")</label></div>
                    @Html.TextBoxFor(m => m.VisitorEmail)
                    @Html.ValidationMessageFor(m => m.VisitorEmail, @Dnn.LocalizeString("VisitorEmailRequired"))
                </div>
                <div class="dnnFormItem">
                    <div><label for="itemDescription">@Dnn.LocalizeString("lblPhone")</label></div>
                    @Html.TextBoxFor(m => m.VisitorPhone)
                </div>
                <div class="dnnFormItem">
                    <div><label for="itemDescription">@Dnn.LocalizeString("lblMessage")</label></div>
                    @Html.TextAreaFor(m => m.VisitorMessage)
                    @Html.ValidationMessageFor(m => m.VisitorMessage, @Dnn.LocalizeString("VisitorMessageRequired"))
                </div>
    
                @Html.HiddenFor(m => m.ModuleId)
            </fieldset>
    
            <button id="btnSumbit" type="button" class="dnnPrimaryAction">@Dnn.LocalizeString("Submit")</button>
            <a id="cancelEdit" href="#" class="dnnSecondaryAction">@Dnn.LocalizeString("Cancel")</a>
        </form>
    
    </div>
    

My index view does get rendered successfully, and I can see my form. Issue is in understanding the way in which I post this data to the MVC Post method. Also my data annotations for validations on my modal class does not work on client side.

I tried posting through jQuery Ajax on the Post method, but that throws an internal error.

Looking forward to hear from DNN community.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
user3276940
  • 487
  • 5
  • 16
  • The official documentation we have for MVC module development is http://www.dnnsoftware.com/docs/developers/about-modules/mvc-module-development.html. You will also find blogs, community projects, and DNNHero.com tutorials that cover the subject. Also, my sample MVC project from my answer gives at least a working example. Have you gotten any further with these resources? – Fix It Scotty May 13 '16 at 13:28
  • @DotNetNuclear : Thanks for responding. I will be resuming my work on this coming weekend. My point is , for e.g. there is .dnn file that I guess is used for routing instead of RouteConfig.cs (as oppose to asp.net MVC). Where is this explained ? How will validations work on client side and server side ? How are data annotations on modal classes for validation tied to client side validations ?. I am an Android developer + a web developer. I see really mature documentation in other frameworks but not DNN – user3276940 May 13 '16 at 16:46

1 Answers1

2

A good DNN8 MVC module sample project is my Restaurant Menu MVC project which you can download from github.

My Edit view and controller has form fields with working validation that you can check out.

Make sure when the submit button is clicked, it is entering your controller method (I assume the view is called FormEntry based on your action method?).

Also, your action method that handles your post should return an action result, not a string.

In my post handler, I return a RedirectToRouteResult after persisting the form data if the ModelState.IsValid is true, otherwise I will return the current View with the same model which will fire the validation.

[HttpPost]
[DotNetNuke.Web.Mvc.Framework.ActionFilters.ValidateAntiForgeryToken]
public ActionResult Edit(MenuItem item)
{
    if (ModelState.IsValid)
    {
        //Persist form information

        //Go to default view (Index)
        return RedirectToDefaultRoute();
    }

    // Return to my edit view if there was an error.
    return View(item);
}
Fix It Scotty
  • 2,852
  • 11
  • 12