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?
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();
}
}
}
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.
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.
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?
Hi ,
Can you share the screen of your form and send function code?
Also try some thing this @using (Html.BeginUmbracoForm
}
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:
Controller:
Partial:
Invoking partial:
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
But I am uisng
So it is exactly what you suggested, but still in my Controller I am getting msg null
Hi,
I defined input like in attached screen short and it works.
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.
Can you maybe send me a screen of how your begining of the form looks like?
Hi,
My view presentation is some thing like define in mention link click here
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.
Hi Asad,
Tried that, but still the same result.
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?
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.
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! :)
is working on a reply...