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
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
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
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
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
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
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
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
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
My solution was using the Template method - you are using Format. Please try the suggested approach.
Regards,Atanas Korchev
the Telerik team
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
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
This is the project I'm using.
Thanks.
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.
Atanas Korchev
the Telerik team
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
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.
Atanas Korchev
the Telerik team
<%: Html.ActionLink("Edit", "About2", new { id = Model.First().Id }) %>
Thanks for your help.
Roberto
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