1

I have the following partial class as a model:

public partial class SourceDevelopmentActivity
{
    public int? ActivityID { get; set; }
    public string Description { get; set; }        
}

I also have the following class to configure the model:

public class SourceDevelopmentActivityMap : EntityTypeConfiguration<SourceDevelopmentActivity>
{
    public SourceDevelopmentActivityMap()
    {
        this.ToTable("SourceDevelopmentActivity");
        this.HasKey(t => t.ActivityID);
        this.Property(t => t.ActivityID).HasColumnName("ActivityID");
    }
}

I pass the configuration into the modelBuilder's configurations on my DBContext's OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new SourceDevelopmentActivityMap());
}

When I use the scaffolding option to create the controller and views (CRUD) the edit form has the following setup:

<div class="editor-label">
    @Html.LabelFor(model => model.ActivityID)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.ActivityID)
    @Html.ValidationMessageFor(model => model.ActivityID)
</div>

If I change my class to use

public int? SourceDevelopmentActivityID { get; set; }

Then instead of getting the EditorFor html helper I get the HiddenFor html helper (which is what I want):

@Html.HiddenFor(model => model.SourceDevelopmentActivityID)

My question is how do I use ActivityID as my property name but get the scaffolding to understand that it should not be displayed on the edit form (as is the case when I use SourceDevelopmentActivityID)? I thought this was a pretty straight forward task which makes me think I'm missing something obvious.

Please let me know if you need more info or have any questions. Thanks in advance for any help!

macoms01
  • 1,110
  • 13
  • 22
  • I don't think there is a straight forward way to do it. But you can add the following attribute [HiddenInput(DisplayValue = false)] to the selected property. And in this way your EditorFor for this property will generate an input hidden instead of text box. – ssimeonov Feb 13 '14 at 19:15
  • @ssimeonov - Okay, thanks for the help. Unfortunately that sounds like it will still be displaying the two divs, the label, and the validator... Which means I'll still have to delete each of those each time I use the scaffolding feature. I'll test it out once I'm back in front of my pc. – macoms01 Feb 13 '14 at 22:55
  • I always use the name 'Id' for the primary key, when I scaffold a form it's always a hidden element. But since I use the `KeyAttribute` as well I'm not sure which one triggers the hidden element. – Marthijn Feb 15 '14 at 23:08

0 Answers0