1

I have a View that I am using as an EditorTemplate. I want to limit what the user can edit vs. what they can insert. My EditorTemplate View is typed as SomeModel and I know that if SomeModel.Id is not 0, that means we are doing an edit, otherwise we are doing an insert. I thought I'd be able to do something like:

@if (Model.Id == 0) 
{ 
  //show "insert-specific" UI 
}

but for some reason, I always get 0's, nulls, default, etc. when I check via Model. whereas the Html helper methods pick up the true value just fine, such as:

@Html.TextBoxFor(model => model.Id)

Again, the value of Model.Id is always 0, even when @Html.TextBoxFor(model => model.Id) shows another value.

Is there a better way to achieve what I am trying to do?

Note: Not sure that it matters, but I'm using the Telerik MVC Grid control. It doesn't seem to allow for a different View for inserting vs. editing.

4

1 回答 1

1

哟,

如果您使用的是 Ajax 绑定,那么 EditorTemplate 会被序列化并发送到客户端,每次您编辑记录或添加新记录时,都会使用不同的值重新填充它。如果您使用的是 ajax 绑定,那么您可以使用 OnEdit 客户端事件并检查e.mode是否为editinsert。然后,您可以使用 JavaScript 操作该编辑器。

于 2012-09-10T17:48:45.353 回答