2

I have a dynamic view, this will display any model that has been passed to it.

@model dynamic

@using (Html.BeginForm("Edit", null, FormMethod.Post, new { id="FrmIndex" }))
{
@Html.ValidationSummary(true);
@Html.EditorForModel()

 <input type="submit" value="Edit" />
}

Say one of my model is PartyRole

public partial class PartyRole
{
    [Key, Display(Name = "Id"]
    [UIHint("Hidden")]
public int PartyRoleId { get; set; }

    [UIHint("TextBox")]
    public string Title { get; set; }

}

I dont want to show Id in edit mode, so I am hiding it in Hidden.cshtml editorfortemplate as below:

 @Html.HiddenFor(m => Model)

This is hiding the editor, but not the label "Id". And I cannot use the answers provided here, How to exclude a field from @Html.EditForModel() but have it show using Html.DisplayForModel()

because IMetadataAware requires System.Web.Mvc namespace which I cannot add in my Biz projects that are having the poco model classes. I cannot use [HiddenInput(DisplayValue = false)] also because this is also party of web.mvc

can somebody give a solution??

Community
  • 1
  • 1
mmssaann
  • 1,507
  • 6
  • 27
  • 55
  • @DylanSlabbinck, I am afraid I didnt get you. thats pretty much are my views. I havent used Label for anywhere. can u elaborate? – mmssaann Jul 25 '13 at 06:45
  • Can you use [ScaffoldColumn(false)]? This will knock the property out of the EditForModel(), but will also knock it out of DisplayForModel(). – Yellowfog Jul 25 '13 at 11:19
  • nope,scoffold doesnt render the column at all. We need the id when we post the data back to controller.so it should be a hidden field on the view. – mmssaann Jul 25 '13 at 11:35

1 Answers1

2

I think that the thing to do is create a custom Object.cshtml editor template, as described in

http://www.headcrash.us/blog/2011/09/custom-display-and-editor-templates-with-asp-net-mvc-3-razor/

(nb. I found How to add assembly in web.config file of mvc 4 to be helpful with the System.Data.EntityState reference.)

Within that template you can put appropriate code to hide the label. The following is a dumb example - I guess that I'd probably try to pick up a custom attribute, though apparently this would involve an overload of DataAnnotationsModelMetadataProvider.

if (prop.HideSurroundingHtml)
{
    @Html.Editor(prop.PropertyName)
}
else if (prop.PropertyName == "PartyRoleId")
{
    <div></div>   
}
else if (!string.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString()))
{
    <div class="editor-label">@Html.Label(prop.PropertyName)</div>
}
Community
  • 1
  • 1
Yellowfog
  • 2,323
  • 2
  • 20
  • 36
  • @Yellowfor,thanks for the reply.I tried the way. But with this some existing functionality was impacted. I have IEnumerable in ViewData, which I am showing it as dropdown in the view using: Html.DropDownList("",(IEnumerable)ViewData[fieldName], "Choose..." , new { class ="combo"}). Now with object.cshtml being added, this dropdown is not being displayed, it is displaying all data as simpletext False False ... like that on my screen. Any ideas?? – mmssaann Jul 26 '13 at 05:35
  • Though I have some other problems, this approach is working fine so I ll vote this. – mmssaann Jul 26 '13 at 09:41
  • Thanks - I'll try to figure out the answer to your other question when I have the time. – Yellowfog Jul 26 '13 at 11:22