Home > c# > Custom templates with EditorFor using nested models

Custom templates with EditorFor using nested models

January 24Hits:1
Advertisement

I have nested ViewModels and I am attempting to use EditorFor in a form. I have a parent VM that contains one or more child VMs. In order to have greater code reuse I have a partial for each child VM with its attached model. Note that the parent may have multiple child VMs and the child VMs may have multiple fields.

public class ParentVM {     public ChildVM Child { get; set; } }  public class ChildVM {     public static SelectList DropDownMenuOptions;     public DropDownMenuChoice { get; set; } } 

My parent view uses EditorFor on the child partial as a custom template and passes in attributes.

@model ParentVM @using (Html.BeginForm("ParentController", "ParentControllerMethod", FormMethod.Post, new { @role = "form" })) {     @Html.EditorFor(m => m.Child, "ChildPartial.cshtml", new { htmlAttributes = new { @class = "form-control" }}) } 

The child view has multiple fields with an editor for those fields. In addition, the model has a static options list for dropdown menus.

@model ChildVM <div class="form-group">     @Html.LabelFor(m => m.DropDownMenuChoice)     @Html.DropDownListFor(m => m.DropDownMenuChoice, ChildVM.DropDownMenuOptions) </div> 

The problem is I am not just displaying the child fields. They are in a web form and I need their values. This only works using EditorFor, but then much of the functionality is removed, including styling individual attributes and accessing the static options list in the child (nothing happens). The workarounds are quickly becoming complex and I need a way to manage it.

I need a way to access the static child fields directly or by passing them in and potentially a better way of styling the elements individually, otherwise I need suggestions on a better way to organize my VMs besides just copy-pasting the fields into multiple parent VMs.

Related Articles

  • Custom templates with EditorFor using nested modelsJanuary 24

    I have nested ViewModels and I am attempting to use EditorFor in a form. I have a parent VM that contains one or more child VMs. In order to have greater code reuse I have a partial for each child VM with its attached model. Note that the parent may

  • How to create SharePoint list with custom template using Managed Client Object Model?December 7

    Creating lists in SharePoint using Managed Client Object Model is rather easy task. Here's how to create a list with SharePoint's custom list template: ListCreationInformation lci; List list; lci = new ListCreationInformation(); lci.Title = title; lc

  • Upload a custom template using client object modelJanuary 18

    I am trying to upload a new custom template to the Template file repository using the Client object model. But I couldn't any provision to add a template as such. So is there any way to do it. Thanks in Advance. Jonathon

  • PHP MVC class with controller and nested model

    PHP MVC class with controller and nested modelNovember 26

    I had asked a first question about this class a while ago and got a few answers here which made me rewrite it completely. I removed all statics and globals, added my variables as arguments for the constructor, and pass them again as reference through

  • Showing sidebar-2 on custom template pageDecember 23

    Environment: WP 3.5, Twenty Twelve child theme. I created a custom template. Now, I'd like to display the built-in sidebar-2 in this custom template page (instead of the default sidebar-1). sidebar-2 is already registered by twenty twelve functions.p

  • Comments not nesed with custom templateMay 15

    I have a custom template for to show my comments. For some reason, there are not nested while looking in the back-end of Wordpress, I see that some comments have a parent. They are simply displayed starting with the most recent on top. So I guess I c

  • Knockout JS Mapping fromJS nested modelsJanuary 27

    I am having trouble understanding how to work with Knockout JS Mapping Plugin. I have some nested models (as seen below) and what I am currently doing is just using the ko.mapping.fromJS() in the parent model. What I am noticing is that computed valu

  • Rails - Why does my nested model form not require 'accepts_nested_attributes_for'February 9

    I have two models item and user_item. Item has many user_items and user_items belongs to item. I have a form where a user can create a new item. In the form a user should include a picture. The name, description, and tags get saved to a new item obje

  • How to set my custom template(*.oft) as a default template in outlook 2007?December 26

    I've created a custom template (.oft) in Outlook 2007. How can I configure it as the default template so that it will appear whenever I click the New button? --------------Solutions------------- you can always put together a short macro that calls up

  • Remove custom template from Word 2003July 1

    When I try to open a certain doc file in MS Word, it takes about 5 minutes to load and then the file is OK to work with. I tried to repair the file using Open and Repair in MS Word 2003 but it did not change anything when I reopened the file. I tried

  • Is there a way to upgrade a theme without losing custom templates?September 15

    I recently updated the main theme of a production site. Unfortunately, the upgrade process deleted all our custom templates. Is there any way I can prevent this happening again? --------------Solutions------------- Child themes might be your best bet

  • How can I make my custom templates respect permissions?September 16

    I use the Members plugin to manage permissions on my site. For standard templates, it works great - when the user doesn't have permission to see a page, they get the following message: Sorry, but you do not have permission to view this content. How c

  • How can I dock ArcToolbox in ArcMap 10 custom template and make it stay?

    How can I dock ArcToolbox in ArcMap 10 custom template and make it stay?September 21

    Here is the situation: Because UI Controls cannot be saved to Normal.mxt like they used to I had to create an empty document with all my custom VBA code that is mostly called from the local document-level UI controls. (Until I have time to get profic

  • How can I load VBA customizations stored in a "document-level custom template" into a Map Document created from a "Normal.mxt" template in ArcGIS 10?September 22

    I have all kinds of custom tools that are called from UI Controls stored in a document-level map document (my custom template). In earlier versions of ArcGIS these were stored in the global-level "Normal.mxt". Since this it is no longer possible

  • How to support custom templates on a custom post type?

    How to support custom templates on a custom post type?October 27

    I'm stucked at some point developing my theme. I created a custom posts types to support an image gallery. So far, it's working great (have a look on 'galeria' on psaikali.com). I registered this custom post type (register_post_type( 'artwork', $args

  • How to add custom template tag in wordpress theme?February 12

    I am working on a WordPress site, it's an online newspaper site. The theme I am developing, I want to add custom template functions like get_footer() or get_header(), so that I can save those HTML and PHP codes like the way footer.php stores. Thanks

  • Custom template for each pageMarch 23

    I am creating multiple pages in Wordpress. I understand I can style these with page.php. Is there any way I can style specific pages with custom templates? So for example if I had page About Us I would want a sidebar that has links to our profiles. I

  • Add custom template page programmaticallyMarch 29

    I'm trying to add template page from a plugin,and my template page is in this plugin folder.Is this posible?Here is my code: global $user_ID; $new_post = array( 'post_title' => 'Test Template Page', 'post_content' => 'Some text', 'post_status' =>

  • Troubles with making a custom template for posts

    Troubles with making a custom template for postsMarch 31

    Is it possible to make a template for all posts in a custom post type? For example I would like to be able to change an adsense ad or some other element on every post by simply editing a custom template. I have been experimenting with templates that

  • Can't get a custom template taxonomy page to displayApril 27

    I've been trying to wade my way through learning the ins and outs of taxonomies and how to integrate them into themes and I've run into a pretty basic issue that I can't seem to figure out. I started working on this in a BuddyPress install using More

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.550 s.