KendoUI Grid Default Value with Data Annotation

故事扮演 提交于 2021-02-18 12:14:47

问题


I am using Kendo UI Grid with ASP.NET MVC Helpers and auto generated columns.

I have [DefaultValue(60 * 60)] annotation in my view model but Kendo helpers doesn't seem to respect that.

Can I have default value specified (probably with data annotations) without having to manually describe the columns?


回答1:


if you defined the columns in the grid manually, you need to set the default value like this despite you defined the default value in the annotation or not

 @(Html.Kendo()
      .Grid()
      .DataSource( d=> d.Ajax()
                        .Model(m=>{
                            m.Field(f=>f.YourField).DefaultValue(YourValue);
                 }))
)

so for the auto-generated columns you can try the following

@(Html.Kendo()
      .Grid()
      .Events( e => e.Edit("onEdit"))
)

<script type="text/javascript">
      function onEdit(e) {   
           //check if record is new
           if (e.model.isNew()) {
                // set the value of the model property like this                    
                e.model.set("PropertyName", Value);
                // for setting all fields, you can loop on 
                // the grid columns names and set the field
           }
    }
</script>

hope this will help you




回答2:


For default values, I like using the constructor which I believe Kendo should new-up an instance of your model, so this would work.

View mode

public class ViewModel
{

    public string Name { get; set; }

    public ViewModel()
    {
        Name = "First name";
    }

}

EDIT

After doing some searching in their docs, it appears that the data annotations or constructor default value are not supported and you must define the default values in the grid definition. See http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/faq#how-do-i-specify-default-property-values-when-a-new-item-is-created for more.



来源:https://stackoverflow.com/questions/27670471/kendoui-grid-default-value-with-data-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!