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

Problems with MVC Grid and CSLA object

17 Answers 175 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.
Roberto
Top achievements
Rank 1
Roberto asked on 25 Oct 2010, 12:52 PM
hello to everyone.
I am evaluating the extension of the grid for mvc with CSLA and I am getting partial success.
My problem happens when I set the command to delete, edit and save.
When running, an exception is thrown with The Message:

"No parameterless constructor defined for this object."

Here's the code:

in index.aspx (view):
...
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Contratos.Lib.ContratoInfoList>" %>
...
  <%: Html.Telerik().Grid(Model)
        .Name("Contratos")
        .DataKeys(keys => keys.Add(c => c.Id))
        .ToolBar(commands=>commands.Insert())
        .DataBinding(dataBinding => dataBinding.Ajax()
            .Select("SelectAjaxBinding", "Home")
        )
        .DataBinding(dataBinding => dataBinding.Server()
            .Select("Index", "Home")
            .Insert("InsertEditing", "Home")
            .Delete("DeleteEditing", "Home")
        )
        .Columns(columns =>
        {
            columns.Bound(c => c.Numero).Title("Número");
            columns.Bound(c => c.Descricao).Title("Descrição");
            columns.Bound(c => c.NomeModalidade).Title("Modalidade");
            columns.Bound(c => c.NomeTipo).Title("Tipo");
            columns.Bound(c => c.DataInicio).Title("Data Inicial").Format("{0:dd/MM/yyyy}").Width(120);
            columns.Bound(c => c.DataFim).Title("Data Final").Format("{0:dd/MM/yyyy}").Width(120);
            columns.Bound(c => c.NomeOrgao).Title("Órgão Contratante");
            columns.Bound(c => c.UASG).Title("UASG");
            columns.Bound(c => c.RazaoSocial).Title("Contratado");
            columns.Command(commands => commands.Delete());
        })
        .Pageable()
        .Sortable()
        .Filterable()
        .Groupable()
    %>
    <%: Html.Telerik().ScriptRegistrar().Globalization(true) %>

...
HomeController in:

...
 [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Sistema de Contratos - SERLLC Superintendência Regional Sul";
            return View(Contratos.Lib.ContratoInfoList.GetContratoInfoList());
        }

        [GridAction]
        public ActionResult SelectAjaxBinding()
        {
            return View(new GridModel(Contratos.Lib.ContratoInfoList.GetContratoInfoList()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertEditing(string id)
        {
            return View(new GridModel(Contratos.Lib.ContratoInfoList.GetContratoInfoList()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteEditing(string id)
        {
            return View(new GridModel(Contratos.Lib.ContratoInfoList.GetContratoInfoList()));
        }

        public ActionResult About()
        {
            return View();
        }
    }
....

What is going wrong with this code?
TIA,
Roberto

17 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 25 Oct 2010, 01:36 PM
Hello Roberto,

 I think the exceptions indicates the problem. The grid needs to instantiate a new object of the model type in order to render the editors using Html.EditorFor. 

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 25 Oct 2010, 01:43 PM
But this occurs before any action is trigged. I only render the index view.
If I disable commands, it´s works fine.
So, where I create a new object of this csla object? On Post ActionResult of this controller?

Regards,
Roberto
0
Atanas Korchev
Telerik team
answered on 25 Oct 2010, 01:51 PM
Hello Roberto,

 The grid instantiates an instance of the model object in order to output the editors of the columns. Your object needs to have parameterless constructor. I am afraid other workarounds are not possible.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 25 Oct 2010, 07:52 PM
Hi, Atanas.
My grid is connected to a data source that allows only reading.
Thus my object does not have a constructor without parameters.
Is there any other way, when you click on a line, an action may be called to supply the necessary data?

regards
roberto
0
Atanas Korchev
Telerik team
answered on 26 Oct 2010, 07:12 AM
Hi Roberto,

 If you want your grid to be editable your model needs to have a parameterless constructor. The grid does not support models that don't have such a constructor if editing is enabled. There is no workaround.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 26 Oct 2010, 03:56 PM
ok.
Let us leave aside the idea of using the grid to edit an object (besides, the object is relatively complex for this operation).
I have a list of some simplified objects that represent a data complex object. In this list, the simple objects are read-only. How do I get when clicking on this object on the grid, the program flow to deviate from an editing view, carrying the id of the object in question? I wish I could use methods on the server side, if possible.

regards
Roberto
0
Atanas Korchev
Telerik team
answered on 26 Oct 2010, 04:04 PM
Hi Roberto,

 You need to create a template column. This will work only if you are using server binding however. Here is a code snippet:


columns.Template(o => 
{
%>
       <%= Html.ActionLink("Edit", "Action", "Controller", new { id = o.OrderID }) %>
<%       
});


Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 27 Oct 2010, 05:11 PM
Hi, Atanas.
I followed your instructions and the initial problem was resolved. However, when I click on the link 'Edit', the contoller that is raised is not getting the id parameter.
See:


the index view:

<%: Html.Telerik().Grid(Model)
                      .Name("Contratos")
                      .DataKeys(keys => keys.Add(c => c.Id).RouteKey("Id"))
                      .DataBinding(dataBinding => dataBinding.Ajax().Select("SelectAjax", "Home"))
                      .DataBinding(dataBinding => dataBinding.Server().Select("Index", "Home"))
                      .Columns(columns =>
                                   {
                                       columns.Bound(c => c.Id).Format(Html.ActionLink("Edit", "About2", "Home", new { id = "{0}"}).ToString()).Encoded(false).Title("Edit").Sortable(false).Groupable(false).Filterable(false);
                                       columns.Bound(c => c.Numero).Title("Número");
                                       columns.Bound(c => c.Descricao).Title("Descrição");
                                       columns.Bound(c => c.NomeModalidade).Title("Modalidade");
                                       columns.Bound(c => c.NomeTipo).Title("Tipo");
                                       columns.Bound(c => c.DataInicio).Title("Data Inicial").Format("{0:dd/MM/yyyy}").Width(120);
                                       columns.Bound(c => c.DataFim).Title("Data Final").Format("{0:dd/MM/yyyy}").Width(120);
                                       columns.Bound(c => c.NomeOrgao).Title("Órgão Contratante");
                                       columns.Bound(c => c.UASG).Title("UASG");
                                       columns.Bound(c => c.RazaoSocial).Title("Contratado");
                                   })
                      .Pageable()
                      .Sortable()
                      .Filterable()
                      .Groupable()
                       
%>
<%: Html.Telerik().ScriptRegistrar().Globalization(true) %>

and the home controller:

public ActionResult About2(Guid id)
        {
            return View(id);
        }

Am I forgetting a step?

Regards,
Roberto



0
Atanas Korchev
Telerik team
answered on 28 Oct 2010, 06:37 AM
Hi Roberto,

 My solution was using the Template method - you are using Format. Please try the suggested approach.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 28 Oct 2010, 10:56 AM
Hi Atanas.
I used the method you suggested, but still keep getting a null in the controller.

columns.Template(o =>
               {%>
                  <%: Html.ActionLink("Edit", "About2", "Home", new { id = o.Id }) %>
               <%});

I need to decorate the method with a parameter?

Regards
Roberto
0
Atanas Korchev
Telerik team
answered on 28 Oct 2010, 10:57 AM
Hello Roberto,

Please attach a sample application showing the problem. The suggested solution should work and I think there is something else that is wrong.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 28 Oct 2010, 11:08 AM
Hello Atanas.
This is the project I'm using.
Thanks.
0
Atanas Korchev
Telerik team
answered on 28 Oct 2010, 11:21 AM
Hi Roberto,

 I cannot run your project because I don't have the database. I cannot instantiate your model either because its constructor is not public. I checked the code and it looks perfectly correct. I suggest you check the generated output for the edit hyperlink. Also check if the Id property returns the correct value. Finally you can try this

<%: Html.ActionLink("Edit", "About2", "Home", new { id = Model.First().Id }) %>

to see if the correct link is generated.

All the best,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 28 Oct 2010, 11:47 AM
Hi Atanas.

This is the link generated:
<td class="t-last">
<a href="/Home/About2?Length=4" id="d5f0253b-1844-4714-9c37-e0ec87e36aad">Edit</a>
</td>

I added the full project
0
Accepted
Atanas Korchev
Telerik team
answered on 28 Oct 2010, 11:56 AM
Hello Roberto,

 This clearly shows that you are not using the right overload of the ActionLink helper method. As you can see the id is rendered as html attribute instead of route value.

 To clarify this is not related to Telerik Grid for ASP.NET MVC.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roberto
Top achievements
Rank 1
answered on 28 Oct 2010, 12:07 PM
You're right, Atanas. The same method used was wrong. The correct is this:

<%: Html.ActionLink("Edit", "About2", new { id = Model.First().Id }) %>

Thanks for your help.
Roberto
0
James
Top achievements
Rank 1
answered on 16 Feb 2011, 04:21 PM
Hi Support,

Some awesome support in this thread. Thumbs up to Telerik here.
Sorry for the resurrection of the old thread but i have further questions.

I'm using datasets, data tables and data rows... Obviously I've run into some similar problems as a row doesn't have a constructor with no parameters.

I've been doing some reading and it seems that i could use a custom DataBinder?
How would i implement this? 
Is it even possible?
Or am i going to have to roll my own class that reflects a row in the datatable?

regards,
James
Tags
Grid
Asked by
Roberto
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Roberto
Top achievements
Rank 1
James
Top achievements
Rank 1
Share this question
or