1

I'm having a weird issue with a form in my View not returning the model's correct Id property value. You'll noticed in the code below I have the script section logging the model's Id. Doing this shows the correct Id on the console, but when the Id is passed to the Controller's action method it is always 0, which is incorrect.

Here's the view:

@model EotE_Encounter.Models.Encounter

<div>
    <h4>@Model.Name</h4>
    <div>
        <form asp-action="CreateCharacter" asp-controller="Encounter" data-bind="" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#character-container">
            <input id="encounterId" type="hidden" value="@Model.Id" />
            <button class="btn btn-default" type="submit">Create Character</button>
        </form>
    </div>
    <hr />
    <div>
        <ul>

            @{
                if(Model.CharactersInEncounter != null)
                {
                    foreach(Character character in Model.CharactersInEncounter)
                    {
                        <li>@character.Name</li>
                    }
                }
            }        
        </ul>
    </div>
</div>

<script>
    console.log(@Model.Id);
</script>

Related Action Method:

public ActionResult CreateCharacter(int encounterID)
        {
            return RedirectToAction("CreateCharacter", "Character", encounterID);
        }

And the Encounter model:

 public class Encounter
    {
        //these first three properties may not be used just yet.
        public int Id { get; set; }
        public string Name { get; set; }
        public byte Round { get; set; }
        public List<Character> CharactersInEncounter { get; set; }
        [StringLength(2000)]
        public string Notes { get; set; }

    }
6
  • 1
    Add your model classes and related action methods to the question please.. Commented Mar 15, 2018 at 18:39
  • Just added them. :) Commented Mar 15, 2018 at 18:43
  • Ok..Now tell me what id value your are expecting? Commented Mar 15, 2018 at 18:44
  • The Id value is created when I add the Encounter to the DB. But for example, the Id was once 16008. This was logged on the console, so I'm pretty sure the model is being passed to the view correctly. However, when the form calls its associated action method, the Id is always 0. Does that make sense? Commented Mar 15, 2018 at 18:48
  • Where is your get method that retrieving the entity from the database? Commented Mar 15, 2018 at 18:53

1 Answer 1

1

Only form elements with a name attribute will have their values passed when submitting a form. So, add the name attribute to your hidden element. Id and the name are not the same.

@model EotE_Encounter.Models.Encounter

<div>
    <h4>@Model.Name</h4>
    <div>
        <form asp-action="CreateCharacter" asp-controller="Encounter" data-bind="" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#character-container">
            <input id="encounterId" name="encounterID" type="hidden" value="@Model.Id" />
            <button class="btn btn-default" type="submit">Create Character</button>
        </form>
    </div>
    <hr />
    <div>
        <ul>

            @{
                if(Model.CharactersInEncounter != null)
                {
                    foreach(Character character in Modelsel.CharactersInEncounter)
                    {
                        <li>@character.Name</li>
                    }
                }
            }        
        </ul>
    </div>
</div>

<script>
    console.log(@Model.Id);
</script>

Notice the name attribute of the <input id="encounterId" name="encounterID" type="hidden" value="@Model.Id" /> element. It has to be the same name as the action parameter (int encounterID). If it's not the same then the parameter binding will not work.

Sign up to request clarification or add additional context in comments.

1 Comment

Not only did I have a typo, which you helped point out, but adding the name attribute did the trick. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.