locked
Navigation Properties don't Httppost from View to Controller RRS feed

  • Question

  • User1434241939 posted

    I have a class whose properties (including a Navigation Property to another table) show up fine in the View.  When I'm editing the properties and hit submit, all the properties get posted to the controller except the Navigation Class.  I tried creating a hidden asp-for"" input tag helper to pass along the Navigation Object, but it doesn't make it to the controller.

    View Model Class (there are more fields, but I cut out a bunch of extraneous ones)

        [Table("bagsmvc")]
        public partial class Bagsmvc
        {
            [Key]
            [Column("id", TypeName = "int(11)")]
            public int Id { get; set; }
            [Column(TypeName = "varchar(255)")]
            [Required]
            public string Airline { get; set; } = "No Airline";
    
            public int? PersonID { get; set; } // Only 1 person obtained from
            public Peoplemvc Person { get; set; } // This is the person bag was obtained from 
        }
    

    And some code from the Edit View

        <form asp-controller="Bags" asp-action="Edit" method="post">
    
                <input type="hidden" asp-for="@Model.Id" />
    
                <label asp-for="@Model.Airline"></label>
                <input asp-for="@Model.Airline" />
    
                <label asp-for="@Model.PersonID"></label>
                <input asp-for="@Model.PersonID">
    
                <input type="hidden" asp-for="@Model.Person" />

    PersonID has a foreign key on another table so I can always get the Person object using .FirstOrDefault() so it's no big deal.  But why doesn't the Person object get posted to the Edit Controller as some kind of payload?

    PS:  I realize that changing PersonID will change the person object.  I'll deal with that separately.

    Sunday, June 7, 2020 3:00 PM

Answers

  • User2078676645 posted

    Hi,

    The value of the input box cannot be an object, but it can be a json string. So if you want to save an object in input, you pass the object as a json string. Controller gets the a parsing conversion is fine.

    Regards,

    Evern

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 2:52 AM

All replies

  • User1120430333 posted

    IMHO, the persistence model is not a viewmodel strong typed to a view or a domain model. So you shouldn't be using the persistence model in the manner you are using it.

    http://blog.sapiensworks.com/post/2012/04/07/Just-Stop-It!-The-Domain-Model-Is-Not-The-Persistence-Model.aspx

    You should be mapping a persistence model object over to a view modelobject and sending the viewmodel object into the view and then map VM object  basc to persistence object for data persistence.

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    The thing  with the navigation is very questionable. 

    Sunday, June 7, 2020 5:15 PM
  • User2078676645 posted

    Hi,

    The value of the input box cannot be an object, but it can be a json string. So if you want to save an object in input, you pass the object as a json string. Controller gets the a parsing conversion is fine.

    Regards,

    Evern

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 2:52 AM
  • User711641945 posted

    Hi stevebo,

    If you use `asp-for` for navigation property,it would generate the property's type as the value in html:

    The correct way is to set hidden input for each property in the navigation property:

    Model:

    public class Peoplemvc
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
    }

    View:

    <input type="hidden" asp-for="@Model.Person.Age" />
    <input type="hidden" asp-for="@Model.Person.Name" />

    Result:

    Best Regards,

    Rena

    Monday, June 8, 2020 6:50 AM
  • User1434241939 posted

    <input type="hidden" asp-for="@Model.Person.Age" />
    <input type="hidden" asp-for="@Model.Person.Name" />

    Thank you, that works!  There are a lot of fields in Person so even though your solution is more intuitive and clearer, the JSON string might be the way to go because it would require less code and is independent of the Person model fields.

    Monday, June 8, 2020 11:02 AM