This is a migrated thread and some comments may be shown as answers.

[Solved] Help with Inline insert error

7 Answers 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Steve
Top achievements
Rank 1
Steve asked on 15 Sep 2010, 03:58 AM
Can someone assist in explaining this error or pointing me to a thread where this is explained? When trying to insert a new record using the grid, I get the following error: "No parameterless constructor is defined for this object", Here is my grid code:

<%      = Html.Telerik().Grid(Model.Messages) 
        .Name("Grid").HtmlAttributes(new { style = "width:90%;" }) 
        .DataKeys(k => k.Add(c => c.MessageId))
        .ToolBar(commands => commands.Insert())
        .DataBinding(dataBinding => dataBinding.Server()
                    .Select("Index", "MessageEditor") 
                    .Insert("Insert", "MessageEditor") 
                    .Update("Save", "MessageEditor") 
                    .Delete("Delete", "MessageEditor")) 
        .Columns(columns =>
            {
                columns.Bound(b => b.MessageId).ReadOnly().Visible(false); 
                columns.Bound(b => b.MessageText);
                columns.Bound(b => b.MessageType);
                columns.Bound(b => b.MessageTypeList).Visible(false); 
                columns.Command(commands =>
                            {
                                    commands.Edit();
                                    commands.Delete();
                            }).Width(200);
            })
        .Editable(e => e.Mode(GridEditMode.InLine)) 
        .Sortable()
        .Pageable()
        .Filterable()
%>

7 Answers, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 1
answered on 15 Sep 2010, 05:11 AM
You need to verify if the action on the controller must or not have a parameter.

Example:

public ActionResult Insert(int parameter)
{
}

or

public ActionResult Insert()
{
}


Can you post your "Insert" action?

Daniel.
0
Joan Vilariño
Top achievements
Rank 2
answered on 15 Sep 2010, 09:35 AM
I guess it's more he needs a parameterless constructor for the Messages property class,
0
Steve
Top achievements
Rank 1
answered on 15 Sep 2010, 05:19 PM
This is my Insert method signature:

[GridAction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Insert()
{
    ...
return RedirectToAction("Index");
}
0
Daniel
Top achievements
Rank 1
answered on 15 Sep 2010, 11:03 PM
@Steve.

The [GridAction] is for Ajax Binding only.

For Server Binding only use

[AcceptVerbs(HttpVerbs.Post)]

Can you post the full Insert Action Result?

Daniel.
0
Joan Vilariño
Top achievements
Rank 2
answered on 16 Sep 2010, 08:08 AM
I'll try to explain myself clearer this time. You are specifying a model for the grid the following way:

= Html.Telerik().Grid(Model.Messages) 

This implies that the entities for each row of your grid will be items of the "Messages" collection.

The error you are getting is because the class of the items contanined in the "Messages" collection doesn't have a parameterless constructor like this:

public MessagesClass() {
}

When you do an insert, the grid tries to create a new item for the messages class calling it's constructor, and as you don't provide (you really can't) any information on his constructor's parameters, it tries to create the new object calling a parameterless constructor that you don't have... that's why that error.

I've had that error myself in my project, and I'm pretty positive the cause is the one I'm trying to explian to you.

Cheers

0
Steve
Top achievements
Rank 1
answered on 16 Sep 2010, 07:25 PM
Thanks Joan,

I was able to fix the problem. This code was originally written by anotehr developer and your post led me to look at the ViewModel. The ViewModel was not created properly.  When I replaced the ViewModel with my own version everything worked as it should. Thanks for the hint on where to look.

0
Joan Vilariño
Top achievements
Rank 2
answered on 17 Sep 2010, 08:04 AM
You're welcome... I had this problem too with some of my ViewModels
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 1
Joan Vilariño
Top achievements
Rank 2
Steve
Top achievements
Rank 1
Share this question
or