Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dmitrij Jazel 19 posts 110 karma points
    Oct 22, 2016 @ 19:57
    Dmitrij Jazel
    0

    Umbraco MVC Action controller receivs null object

    Hi guys,

    Playing around with Umbraco, and trying to build a contact form :) pretty simple.

    Here is what I have:

    1) Umbraco MVC project installed through Nuget.

    2) Message model with 4 string properties

    3) Controller that implements : Umbraco.Web.Mvc.SurfaceController

    4) @inherits Umbraco.Web.Mvc.UmbracoViewPage

    5) using Umbraco form @using (Html.BeginUmbracoForm("Send", "ContactForm"))

    6) Lastly, this is how I invoke partial @Html.Partial("_ContactForm", new TestProject.Models.Message())

    As a result I am invoking correct Action controller, and all seems to be working just fine. But for some reason I am getting null as Models.Message object.

    Any tips, ideas, suggestions what can be wrong here?

  • asad 39 posts 113 karma points
    Oct 22, 2016 @ 20:09
    asad
    0

    Hi ,

    Can you share the screen of your form and send function code?

    Also try some thing this @using (Html.BeginUmbracoForm

    }

  • Dmitrij Jazel 19 posts 110 karma points
    Oct 22, 2016 @ 20:26
    Dmitrij Jazel
    0

    Hi Asad,

    Would you like to see the Rendered html (page source) or the VS project code of this form? Here I attach my codes

    Model:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace Testproject.Models
    {
        public class Message
        {
            public string Name { get; set; }
            public string Phone { get; set; }
            public string EmailAddress { get; set; }
            public string Msg { get; set; }
    
            public int ThankYouPage { get; set; }
    
        }
    }
    

    Controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using BalticPartner.Models;
    using System.Net.Mail;
    
    namespace Testproject.Controllers
    {
        public class ContactFormController : Umbraco.Web.Mvc.SurfaceController
        {
            // GET: ContactForm
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Send(Message msg)
            {
                //ISSUE: msg is null
                if (true) //ModelState.IsValid
                {
                    string s = msg.Msg; // null pointer exception!!!
    
    
                }
                return View();
            }
        }
    }
    

    Partial:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Testproject.Models.Message>
    
    @using (Html.BeginUmbracoForm("Send", "ContactForm"))
    {
    
            <div class="row control-group">
                <div class="form-group col-xs-12 floating-label-form-group controls">
                    <label>Name</label>
                    @Html.TextBoxFor(x => x.Name, new { @class = "form-control", @id = "name", placeholder = "Name", @data_validation_required_message = "Please enter your name.", required = "required" })                
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <div class="row control-group">
                <div class="form-group col-xs-12 floating-label-form-group controls">
                    <label>Email Address</label>
                    @Html.TextBoxFor(x => x.EmailAddress, new { @class = "form-control", @id = "email", placeholder = "Email Address", @data_validation_required_message = "Please enter your email address.", required = "required" })
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <div class="row control-group">
                <div class="form-group col-xs-12 floating-label-form-group controls">
                    <label>Phone Number</label>
                    @Html.TextBoxFor(x => x.Phone, new { @class = "form-control", @id = "phone", placeholder = "Phone Number", @data_validation_required_message = "Please enter your phone number.", required = "required" })
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <div class="row control-group">
                <div class="form-group col-xs-12 floating-label-form-group controls">
                    <label>Message</label>
                    @Html.TextAreaFor(x => x.Msg, new { @class = "form-control", @id = "message", placeholder = "Message", @data_validation_required_message = "Please enter a message.", rows = "5", required = "required" })
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <br>
            <div id="success"></div>
            <div class="row">
                <div class="form-group col-xs-12">
                    <button type="submit" class="btn btn-success btn-lg">Send</button>
                </div>
            </div>
    
    }
    

    Invoking partial:

    <div class="row">
        <div class="col-lg-8 col-lg-offset-2">      
            @{
                @Html.Partial("_ContactForm", new Testproject.Models.Message())
            }
        </div>
    </div>
    

    Hope this brings better light over what I am trying to do here :)

    Thanks for suggestion so far, I will give it a try! Meanwhile if you have any more suggestions - just go ahead :)

    /Cheers

  • Dmitrij Jazel 19 posts 110 karma points
    Oct 22, 2016 @ 20:30
    Dmitrij Jazel
    0

    But I am uisng

    @using (Html.BeginUmbracoForm("Action","Controller"))
    {
     // form data
    }
    

    So it is exactly what you suggested, but still in my Controller I am getting msg null

  • asad 39 posts 113 karma points
    Oct 22, 2016 @ 21:42
    asad
    0

    Hi,

    I defined input like in attached screen short and it works.

    enter image description here

    If you want to use form as you defined then i suggest you to see content hijacking concept in umbraco. Then you will find out the issue easily.

  • Dmitrij Jazel 19 posts 110 karma points
    Oct 22, 2016 @ 21:57
    Dmitrij Jazel
    0

    Can you maybe send me a screen of how your begining of the form looks like?

  • asad 39 posts 113 karma points
    Oct 23, 2016 @ 05:37
    asad
    0

    Hi,

    My view presentation is some thing like define in mention link click here

  • Yasir Butt 161 posts 371 karma points
    Oct 23, 2016 @ 18:04
    Yasir Butt
    0

    i just took a look. first thing i noticed that you should have @model Message at the top instead on inherits view page.

    Because of inherits view page, your form is not posting the Message model.

  • Dmitrij Jazel 19 posts 110 karma points
    Oct 23, 2016 @ 22:27
    Dmitrij Jazel
    0

    Hi Asad,

    Tried that, but still the same result.

    @using (Html.BeginUmbracoForm<Testproject.Controllers.ContactFormController>("Send")) {} 
    

    Invokes same action, but msg is still null

    Hi Yasir,

    Good observation, but same result, msg is still null. This is how I call the partial, is there something wrong with the way I am callling it?

    @Html.Partial("_ContactForm", new Testproject.Models.Message())
    
  • Yasir Butt 161 posts 371 karma points
    Oct 24, 2016 @ 00:51
    Yasir Butt
    1

    I think that i found the error. you have no reference of your Message Model in your controller while you have Net.Mail reference. So message object is coming from mail class instead of your custom Message Model.

    Please check the reference and make it correct.

  • Dmitrij Jazel 19 posts 110 karma points
    Oct 24, 2016 @ 09:54
    Dmitrij Jazel
    0

    Hi Yasir,

    Apparently something was wrong with class reference. It was bad idea to use "Message" as class, that is why I used Msg for Model class, and updated Model class reference in View and controller.

    And it worked! :)

    Thanks allot for help! :)

Please Sign in or register to post replies

Write your reply to:

Draft