display-templates

How to exclude a field from @Html.EditForModel() but have it show using Html.DisplayForModel()

假装没事ソ 提交于 2019-11-30 09:56:06
I am reading up on ASP.NET MVC and all of it's fun uses and I just found out about DataTemplates . In my hurry to test this thing out, I converted one of my simpler models over to using @Html.DisplayForModel() and @Html.EditForModel() and it worked like a lucky charm that it is :) One thing that I immediately found out though was that I could not easily define a field to show up on display views but not be present at all for editing... You can make use of IMetadataAware interface an create attribute which will set ShowForEdit and ShowForDislay in Metadata: [AttributeUsage(AttributeTargets

How to exclude a field from @Html.EditForModel() but have it show using Html.DisplayForModel()

谁说我不能喝 提交于 2019-11-29 15:03:36
问题 I am reading up on ASP.NET MVC and all of it's fun uses and I just found out about DataTemplates. In my hurry to test this thing out, I converted one of my simpler models over to using @Html.DisplayForModel() and @Html.EditForModel() and it worked like a lucky charm that it is :) One thing that I immediately found out though was that I could not easily define a field to show up on display views but not be present at all for editing... 回答1: You can make use of IMetadataAware interface an

Using a DisplayTemplate (with DisplayFor) for each item in a collection

主宰稳场 提交于 2019-11-27 21:15:11
I have created a DisplayTemplate for a Comment class, and placed it inside Comment/DisplayTemplates/Comment.cshtml . Comment.cshtml is properly typed: @model Comment Then, I have a partial view that takes an IEnumerable<Comment> for model. In there I loop through the collection and would like to use the DisplayTemplate for the Comment class. The view, in its integrity: @model IEnumerable<Comment> @foreach (var comment in Model.Where(c => c.Parent == null)) { @Html.DisplayFor(model => comment) } However, I get an error on the Html.DisplayFor line: The model item passed into the dictionary is of

ASP.net MVC - Display Template for a collection

走远了吗. 提交于 2019-11-27 07:23:49
I have the following model in MVC: public class ParentModel { public string Property1 { get; set; } public string Property2 { get; set; } public IEnumerable<ChildModel> Children { get; set; } } When I want to display all of the children for the parent model I can do: @Html.DisplayFor(m => m.Children) I can then create a ChildModel.cshtml display template and the DisplayFor will automatically iterate over the list. What if I want to create a custom template for IEnumerable? @model IEnumerable<ChildModel> <table> <tr> <th>Property 1</th> <th>Property 2</th> </tr> ... </table> How can I create a

In an Editor Template call another Editor Template with the same Model

不想你离开。 提交于 2019-11-26 20:36:02
问题 I have an editor template and within that editor template i want to call another editor template with the same model (i.e. nested), but it does not seem to display. ie. \EditorTemplates\Template1.cshtml @model foo // insert code here to edit the default fields. // display extra fields via another editor template. @Html.EditorForModel("Template2") // or @Html.EditorFor(m => m, "Template2") and \EditorTemplates\Template2.cshtml @model foo @Html.TextBoxFor(m => m.Name) I am sure someone will

Using a DisplayTemplate (with DisplayFor) for each item in a collection

一个人想着一个人 提交于 2019-11-26 20:35:52
问题 I have created a DisplayTemplate for a Comment class, and placed it inside Comment/DisplayTemplates/Comment.cshtml . Comment.cshtml is properly typed: @model Comment Then, I have a partial view that takes an IEnumerable<Comment> for model. In there I loop through the collection and would like to use the DisplayTemplate for the Comment class. The view, in its integrity: @model IEnumerable<Comment> @foreach (var comment in Model.Where(c => c.Parent == null)) { @Html.DisplayFor(model => comment)

ASP.net MVC - Display Template for a collection

谁都会走 提交于 2019-11-26 13:09:17
问题 I have the following model in MVC: public class ParentModel { public string Property1 { get; set; } public string Property2 { get; set; } public IEnumerable<ChildModel> Children { get; set; } } When I want to display all of the children for the parent model I can do: @Html.DisplayFor(m => m.Children) I can then create a ChildModel.cshtml display template and the DisplayFor will automatically iterate over the list. What if I want to create a custom template for IEnumerable? @model IEnumerable