C# 数据绑定细节网格时使用MVC包装器的剑道UI的最佳实践
我想知道在使用KendoUI和MVC时如何最好地构建详细的网格视图/控制器。我的详细模型如下所示:C# 数据绑定细节网格时使用MVC包装器的剑道UI的最佳实践,c#,asp.net-mvc-4,kendo-grid,model-binding,C#,Asp.net Mvc 4,Kendo Grid,Model Binding,我想知道在使用KendoUI和MVC时如何最好地构建详细的网格视图/控制器。我的详细模型如下所示: public class EditablePinNote { [ScaffoldColumn(false)] public int PinId { get; set; } public int NoteId { get; set; } [Editable(false)] public int? SaleYear { get; set; } publi
public class EditablePinNote
{
[ScaffoldColumn(false)]
public int PinId { get; set; }
public int NoteId { get; set; }
[Editable(false)]
public int? SaleYear { get; set; }
public string Note { get; set; }
}
看法
这很有效,似乎应该有更好的方法。我不喜欢的部分在控制器中。需要传递参数pPinId和pSaleYear,因为没有办法(至少我不知道在视图中如何将它们添加到模型中)。此外,如果使用与模型中的字段名相同的名称调用参数,则模型绑定将不起作用,因此需要调用不同的名称才能使模型绑定起作用。总之,我想在视图中设置PinId和SaleYear的模型值,然后控制器会有一个很好的签名,比如Create([DataSourceRequest]DataSourceRequest请求,EditablePinNote模型)。如果可以使用构造函数或以某种方式从详图视图代码在详图模型中设置字段值,则效果会更好。您尝试过这个吗?:
.Model(model =>
{
model.Id(p => p.NoteId);
model.Field(p => p.PinId).DefaultValue("#=PinId#");
})
我在自己的应用程序中也做了类似的事情。Telerik允许传递参数 此示例具有硬编码的值,但您可以使用
GetElementByID
从视图中的字段中获取值,并将其用作参数的值,而不是硬编码的值
@(Html.Kendo().Grid<RoutingInquiryModel>()
.Name("RoutingInquirys")
.Columns(c =>
{
c.Bound(p => p.TextValue);
c.Bound(p => p.DataValue);
c.Bound(p => p.RoutingDescription);
c.Bound(p => p.RoutingRev);
})
.DataSource(d => d
.Ajax()
.Read(read => read.Action("get", "RoutingInquirys",
new { pPlant = "PL2" } ) )
.PageSize(12)
)
.Pageable()
.Filterable()
.Selectable()
.Sortable()
.Groupable()
.Scrollable()
.Resizable(resize => resize.Columns(true))
)
pPlant
是控制器中使用的参数的名称。对于多个参数,它们是逗号分隔的。除了向客户询问之外,您是否可以通过其他方式获取PinId和SaleYear?否,这是从父级传递的详细视图。可以说,这些是天生的钥匙。我没有,但我会的。解决了我不得不更换控制器的问题。然而,不知何故,使用默认值似乎不太正确(Telrik应该为此提供一种机制),但你用比我更好的解决方案回答了我的问题,因此你得到了我的投票。
.Model(model =>
{
model.Id(p => p.NoteId);
model.Field(p => p.PinId).DefaultValue("#=PinId#");
})
@(Html.Kendo().Grid<RoutingInquiryModel>()
.Name("RoutingInquirys")
.Columns(c =>
{
c.Bound(p => p.TextValue);
c.Bound(p => p.DataValue);
c.Bound(p => p.RoutingDescription);
c.Bound(p => p.RoutingRev);
})
.DataSource(d => d
.Ajax()
.Read(read => read.Action("get", "RoutingInquirys",
new { pPlant = "PL2" } ) )
.PageSize(12)
)
.Pageable()
.Filterable()
.Selectable()
.Sortable()
.Groupable()
.Scrollable()
.Resizable(resize => resize.Columns(true))
)
public ActionResult Get([DataSourceRequest] DataSourceRequest request, String pPlant)
{
...