Retriving form values

Topics: Core, Customizing Orchard
Jul 29, 2013 at 9:57 PM
Edited Jul 29, 2013 at 10:05 PM
I have a View which consists of three different models. Of which two are pulling data from Taxonomy fields and displaying the data in dropdowns. Rest of the fields are coming from a Model which inherits ContentPart<T>. How can I retrieve the form values in my Submit ActionResult? And which model should I use to assign the form values? Whenever I am trying to set the values to Contact model I am getting NullReference exception from "Record" part of the properties.

The Contact model inheriting ContentPart<T>
public class Contact : ContentPart<ContactRecord>
{
        public string FirstName
        {
            get { return Record.FirstName; }
            set { Record.FirstName = value; }
        }

        [Required]
        public string LastName
        {
            get { return Record.LastName; }
            set { Record.LastName = value; }
        }

        .......
}
And the ContactRecord class looks like which is inheriting ContentPartRecord.
public class ContactRecord : ContentPartRecord {
        public virtual string FirstName{ get; set; }
        public virtual string LastName{ get; set; }
        public virtual string Email{ get; set; }
        public virtual string JobTitle{ get; set; }
        public virtual string Organization{ get; set; }
        public virtual string Department{ get; set; }
        public virtual string Phone{ get; set; }
        public virtual bool OptIn{ get; set; }
        public virtual bool PostPermission{ get; set; }
        public virtual string Message{ get; set; }
        public virtual string Source{ get; set; }
    }
I have written Submit action method like this:
[HttpPost]
[ValidateAntiForgeryToken]
[Themed]
public ActionResult SubmitFrom(ContactCompositeModel model)
{
    //I am always geting NullException at get { return Record.FirstName; }
    //in Contact controller.
    var fName = model.ContactModel.FirstName;
    return View("ContactUsForm", ContactCompositeModel);
}
I must be doing something wrong. Please help!

Subrata
Jul 29, 2013 at 10:05 PM
Could you show your Submit action method and what parameter(s) it is taking? What's not working?

--


2013/7/29 subratasarkar <[email removed]>

From: subratasarkar

I have a View which consists of three different models. Of which two are pulling data from Taxonomy fields and displaying the data in dropdowns. Rest of the fields are coming from a Model which inherits ContentPart<T>. How can I retrieve the form values in my Submit Action result?

The Contact model inheriting ContentPart<T>
public class Contact : ContentPart<ContactRecord>
{
        public string FirstName
        {
            get { return Record.FirstName; }
            set { Record.FirstName = value; }
        }

        [Required]
        public string LastName
        {
            get { return Record.LastName; }
            set { Record.LastName = value; }
        }

        .......
}
And the ContactRecord class looks like which is inheriting ContentPartRecord.
public class ContactRecord : ContentPartRecord {
        public virtual string FirstName{ get; set; }
        public virtual string LastName{ get; set; }
        public virtual string Email{ get; set; }
        public virtual string JobTitle{ get; set; }
        public virtual string Organization{ get; set; }
        public virtual string Department{ get; set; }
        public virtual string Phone{ get; set; }
        public virtual bool OptIn{ get; set; }
        public virtual bool PostPermission{ get; set; }
        public virtual string Message{ get; set; }
        public virtual string Source{ get; set; }
    }
Please help!

Subrata

Read the full discussion online.

To add a post to this discussion, reply to this email ([email removed])

To start a new discussion for this project, email [email removed]

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Jul 29, 2013 at 10:06 PM
Yes, just edited the post.
Jul 29, 2013 at 10:10 PM
Forgot to include the composite model:
namespace Emfluence.Intrust.Models
{
    public class ContactCompositeModel
    {
        public Contact ContactModel { get; set; }
        public IndustryViewModel IndustryView { get; set; }
        public RegionViewModel RegionView { get; set; }
    }
}
Jul 30, 2013 at 2:18 PM
Hello, is anybody there?
Jul 30, 2013 at 2:49 PM
Edited Jul 30, 2013 at 2:50 PM
You should create one only ViewModel with the exact fields that you need in the form.
On the controller get action you send the this view model.
On the controller post action you can retrieve the form values a inclusively do model binding on this ViewModel.
You have to inject on the controller the repository service for your models, and then do the create the logic to pass the values from the ViewModel to the Models you need, and then save or retrieve data.

Hope this helps.
Developer
Jul 30, 2013 at 3:30 PM
Edited Jul 30, 2013 at 3:31 PM
It looks like you have some concepts slightly wrong.

I would avoid creating a Model with ViewModels within it. Think of a ViewModel as a strongly typed view or template in orchards case?

What it looks like you have done is gone like this Model -> ViewModel/Model -> Model -> Confusion.

You have a Record and a Part, so why not user the BuildEditor() and UpdateEditor() methods to build and update the parts as required? this way you don't need a View model at all and you have a much clearer composition.

Take a look at the way parts are done in other modules, i.e. Blogs.

Also rename the class Contact to ContactPart - that will then fall in line with the current Orchard conventions and help you maintain familiarity across the codebase.