Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using MVC2 you can achieve this.</p> <p>Here is the strongly typed view:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MvcLearner.Models.Person&gt;" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt; Create &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt;Create&lt;/h2&gt; &lt;% using (Html.BeginForm()) { %&gt; &lt;%= Html.LabelFor(person =&gt; person.Name) %&gt;&lt;br /&gt; &lt;%= Html.EditorFor(person =&gt; person.Name) %&gt;&lt;br /&gt; &lt;%= Html.LabelFor(person =&gt; person.Age) %&gt;&lt;br /&gt; &lt;%= Html.EditorFor(person =&gt; person.Age) %&gt;&lt;br /&gt; &lt;% foreach (String FavoriteFoods in Model.FavoriteFoods) { %&gt; &lt;%= Html.LabelFor(food =&gt; FavoriteFoods) %&gt;&lt;br /&gt; &lt;%= Html.EditorFor(food =&gt; FavoriteFoods)%&gt;&lt;br /&gt; &lt;% } %&gt; &lt;%= Html.EditorFor(person =&gt; person.Birthday, "TwoPart") %&gt; &lt;input type="submit" value="Submit" /&gt; &lt;% } %&gt; &lt;/asp:Content&gt; </code></pre> <p>Here is the strongly typed view for the child class (which must be stored in a subfolder of the view directory called EditorTemplates):</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;MvcLearner.Models.TwoPart&gt;" %&gt; &lt;%= Html.LabelFor(birthday =&gt; birthday.Day) %&gt;&lt;br /&gt; &lt;%= Html.EditorFor(birthday =&gt; birthday.Day) %&gt;&lt;br /&gt; &lt;%= Html.LabelFor(birthday =&gt; birthday.Month) %&gt;&lt;br /&gt; &lt;%= Html.EditorFor(birthday =&gt; birthday.Month) %&gt;&lt;br /&gt; </code></pre> <p>Here is the controller:</p> <pre><code>public class PersonController : Controller { // // GET: /Person/ [AcceptVerbs(HttpVerbs.Get)] public ActionResult Index() { return View(); } [AcceptVerbs(HttpVerbs.Get)] public ActionResult Create() { Person person = new Person(); person.FavoriteFoods.Add("Sushi"); return View(person); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Person person) { return View(person); } } </code></pre> <p>Here are the custom classes:</p> <pre><code>public class Person { public String Name { get; set; } public Int32 Age { get; set; } public List&lt;String&gt; FavoriteFoods { get; set; } public TwoPart Birthday { get; set; } public Person() { this.FavoriteFoods = new List&lt;String&gt;(); this.Birthday = new TwoPart(); } } public class TwoPart { public Int32 Day { get; set; } public Int32 Month { get; set; } } </code></pre> <p>And the output source:</p> <pre><code>&lt;form action="/Person/Create" method="post"&gt;&lt;label for="Name"&gt;Name&lt;/label&gt;&lt;br /&gt; &lt;input class="text-box single-line" id="Name" name="Name" type="text" value="" /&gt;&lt;br /&gt; &lt;label for="Age"&gt;Age&lt;/label&gt;&lt;br /&gt; &lt;input class="text-box single-line" id="Age" name="Age" type="text" value="0" /&gt;&lt;br /&gt; &lt;label for="FavoriteFoods"&gt;FavoriteFoods&lt;/label&gt;&lt;br /&gt; &lt;input class="text-box single-line" id="FavoriteFoods" name="FavoriteFoods" type="text" value="Sushi" /&gt;&lt;br /&gt; &lt;label for="Birthday_Day"&gt;Day&lt;/label&gt;&lt;br /&gt; &lt;input class="text-box single-line" id="Birthday_Day" name="Birthday.Day" type="text" value="0" /&gt;&lt;br /&gt; &lt;label for="Birthday_Month"&gt;Month&lt;/label&gt;&lt;br /&gt; &lt;input class="text-box single-line" id="Birthday_Month" name="Birthday.Month" type="text" value="0" /&gt;&lt;br /&gt; &lt;input type="submit" value="Submit" /&gt; &lt;/form&gt; </code></pre> <p>Now this is complete. Set a breakpoint in the Create Post controller action to verify. Don't use this with lists however because it wont work. See my question on using EditorTemplates with IEnumerable for more on that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload