2013-07-17 24 views
0

我想使用EditorForModel()来生成我的视图,但我想要对数据类型进行一些控制,但仅限于某些给定的字段。什么试图做的是为我的ID字段设置一个自定义类型,我不想被编辑。到目前为止,我无法让我的模板对我的自定义数据类型生效。使用自定义数据类型来更改EditorFor MVC4

所以我的模型

public static customer Createcustomer(global::System.Int32 id, global::System.String code, global::System.Boolean data, global::System.Int32 vehtotal) 
    { 
     customer customer = new customer(); 
     customer.id = id; 
     customer.code = code; 
     customer.data = data; 
     customer.vehtotal = vehtotal; 
     return customer; 
    } 

    #endregion 

    #region Primitive Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    [DataType("myCustomID")] 
    public global::System.Int32 id 
    { 
     get 
     { 
      return _id; 
     } 
     set 
     { 
      OnidChanging(value); 
      ReportPropertyChanging("id"); 
      _id = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("id"); 
      OnidChanged(); 
     } 
    } 

我查看

@model Models.customer 

@{ 
    ViewBag.Title = "Edit"; 
} 


<h2>Edit</h2> 

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>customer</legend> 

    @Html.EditorForModel() 

<p> 
     <input type="submit" value="Save" /> 
    </p> 
</fieldset> 

}

我对的自订实地查看

@model .Models.customer 


@{ 
ViewBag.Title = "myCustomID"; 
} 

<h2>myCustomID</h2> 

<p> 
@Html.EditorFor(model => model.id) 

</p> 

回答

0

什么香港专业教育学院试图做是设置为我的ID字段我不想为可编辑的自定义类型

您可以与[HiddenInput]属性装饰它:

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
[DataMemberAttribute()] 
[HiddenInput(DisplayValue = false)] 
public global::System.Int32 id 

注意,这仍然将包括外地但一个隐藏的领域。用户仍然可以伪造请求并更改其值。不要将此用于任何安全声明。

相关问题