Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC Postback from View to Controller shows null values
    primarykey
    data
    text
    <p>I've a problem with ViewModel posting back to a controller, but the ViewModel not being mapped correctly from the View to the Controller.</p> <p><strong>TopicId</strong> and <strong>Content</strong> should contain values, however, when posted back, they do not:</p> <p><strong>VS Debug:</strong> <img src="https://i.stack.imgur.com/TZy09.png" alt="ss"></p> <p><strong>ViewModels:</strong></p> <pre><code> public class PostViewModel { public int PostId { get; set; } public string Title { get; set; } public string Description { get; set; } public string Author { get; set; } public DateTime DateOfTopic { get; set; } } public class ReplyViewModel { public int TopicId { get; set; } public string Content { get; set; } } public class PostListAndReplyVM { public List&lt;PostViewModel&gt; PostViewModel { get; set; } public ReplyViewModel ReplyViewModel { get; set; } } </code></pre> <p><strong>View:</strong></p> <pre><code>@model centreforum.Models.PostListAndReplyVM @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Post&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.ReplyViewModel.TopicId) &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.ReplyViewModel.Content) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.ReplyViewModel.Content) @Html.ValidationMessageFor(model =&gt; model.ReplyViewModel.Content) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; </code></pre> <p>}</p> <p><strong>Generated HTML:</strong></p> <pre><code>&lt;form action="/Post/List/7/" method="post"&gt;&lt;input name="__RequestVerificationToken" type="hidden" value="xxxxxxxxxxxxx" /&gt; &lt;fieldset&gt; &lt;legend&gt;Post&lt;/legend&gt; &lt;input data-val="true" data-val-number="The field TopicId must be a number." data-val-required="The TopicId field is required." id="ReplyViewModel_TopicId" name="ReplyViewModel.TopicId" type="hidden" value="7" /&gt; &lt;div class="editor-label"&gt; &lt;label for="ReplyViewModel_Content"&gt;Content&lt;/label&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;input class="text-box single-line" id="ReplyViewModel_Content" name="ReplyViewModel.Content" type="text" value="" /&gt; &lt;span class="field-validation-valid" data-valmsg-for="ReplyViewModel.Content" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>As you can see from the generated HTML, the TopicId definitely has a value: <code>value="7"</code></p> <p>Can anyone see where the problem is between the form post, and the controller, which is expecting the ReplyViewModel?</p> <p>Thank you,</p> <p>Mark</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.
 

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