Updating value of a field in a custom module

Topics: Writing modules
Apr 9, 2012 at 4:09 PM

I am writing a module that would let users add a field that allows them to select a role from the list of roles available in the system.

I started this by creating a new field type called RolePicker which would essentially be a dropdown. SO, far I have been able to display a dropdown list with some values. However, I am stuck at when the the field is updated. It doesn't save the value chosen in the dropdown. When I debug, I get an exception "System.MissingMethodException: {"No parameterless constructor defined for this object."}". 

This gets thrown when below method gets executed

 bool IUpdateModel.TryUpdateModel(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
            return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
        }

 

-  

My view is like this - 

@model CustomFields.ViewModels.RolePickerFieldViewModel
@using Orchard.Utility.Extensions;
@using CustomFields.Settings;
@{
    //var settings = Model.PartFieldDefinition.Settings.GetModel<RolePickerFieldSettings>();
}
<fieldset>
    <label for="@Html.FieldIdFor(m => Model.Name)">@Model.Name</label>
 @Html.DropDownListFor(m=>Model.RolesList, (SelectList) Model.RolesList)
</fieldset>

Can anyone please guide me on what is missing.

Developer
Apr 9, 2012 at 7:28 PM

Parts and records should have a parameterless constructor. Is this your case?

Apr 9, 2012 at 8:40 PM

Yes, it is a part that I created for testing my RolePicker field from the UI. This part has one Boolen, datepicker and one my RolePicker field.

I am following Orchard.fields module to develop my own module. I am doing exactly how the Orchard module are coded but getting this exception in my module. 

Any idea?

Developer
Apr 9, 2012 at 9:18 PM

Hmm, then I have no idea. This is typically the error if the object that you want to update (i.e. the model) has a constructor that accepts parameters.

Apr 9, 2012 at 10:14 PM

Any one else has any clue??

Apr 10, 2012 at 2:57 AM

viewmodel should have parameterless constructor as well.

Apr 10, 2012 at 5:01 PM

I tried as mentioned above, but it still doesn't work.

Another thing I want to mention here is that when I see my model, all the properties carry null values and not the values as selected in the view.



bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
            return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
        }

The model has all its properties as null like 

Name       null

RolesList   null

Developer
Apr 11, 2012 at 9:03 AM

That's because update fails with the exception. Could you show the model class you're trying to update?

Apr 11, 2012 at 5:12 PM

Thanks everyone for your help! I figured it out. My view had an issue while passing back the values to the controller. The field names weren't correctly matched.

Its fixed now.

Thanks