locked
Two models in one view RRS feed

  • Question

  • User-501297529 posted

    I have two models: Response and Item. I want to use properties from both models. How do i do that:

    public partial class Response
        {
    
            /// <summary>
            /// Gets or sets a specific/unique ID for a certain response (Primary Key --> Integer)
            /// </summary>
            public int ResponseId { get; set; }
    
            /// <summary>
            /// Gets or sets a specific ID for a specific discussion (Integer)
            /// </summary>
            public int DiscussionId { get; set; }
    
            /// <summary>
            /// Gets or sets a specific ID for a certain User (Integer)
            /// </summary>
            public Guid UserId { get; set; }
    
            /// <summary>
            /// Gets or sets the response text (Varchar)
            /// </summary>
            public string Responses { get; set; }
    
            /// <summary>
            /// Gets or sets the date in which a response is created (Date)
            /// </summary>
            public DateTime DateCreated { get; set; }
    
           
            /// <summary>
            /// Gets or sets Discussions of type Discussion 
            /// </summary>
            public Discussion Discussions { get; set; }
        }
    public partial class Item
        {
            public Item()
            {
                Discussions = new HashSet<Discussion>();
                Votes = new HashSet<Vote>();
            }
    
            /// <summary>
            /// Gets or sets a specific/unique ID for a certain item (Primary Key --> Integer)
            /// </summary>
            public int ItemId { get; set; }
    
            /// <summary>
            /// Gets or sets a specific ID for a certain Packet (Foreign Key --> Integer)
            /// </summary>
            public int? PacketId { get; set; }
    
            /// <summary>
            /// Gets or sets a specific ID for a certain category (Foreign Key --> Integer)
            /// </summary>
            public int? CategoryId { get; set; }
    
            /// <summary>
            /// Gets or sets a specific ID for a certain Voting Type (Foreign Key --> Integer)
            /// </summary>
            public int? VotingTypeId { get; set; }
    
            /// <summary>
            /// Gets or sets a specific Title for am Item (Varchar)
            /// </summary>
            public string ItemTitle { get; set; }
    
            /// <summary>
            /// Gets or sets a specific order in which an item will be displayed (Integer)
            /// </summary>
            public int? DisplayOrder { get; set; }
    
            /// <summary>
            /// Gets or sets a specific File name for a certain item (Varchar)
            /// </summary>
            public string ItemFileName { get; set; }
    
            /// <summary>
            /// Gets or sets a specific File URL for a certain item (Varchar)
            /// </summary>
            public string ItemFileUrl { get; set; }
    
            /// <summary>
            /// Gets or sets a specific HTML URL for a specific item (Varchar)
            /// </summary>
            public string ItemHtmlUrl { get; set; }     
           
    
        }

    I know using the two @model tags at the top is not the correct way but don't know how to bring in two models in one view.

    View:

    @model F.E.Domain.Models.Response
    @model F.E.Domain.Models.Item
    
    @{
        ViewData["Title"] = "Board Meeting";
        //Layout = "~/Views/EditProfile/Index.cshtml";
    }
    
    <h2>Current Board Meeting</h2>
    
    
       
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label asp-for="DateCreated" class="control-label"></label>
                    <input asp-for="DateCreated" type="date" asp-format="{0:MM/dd/yyyy}" class="form-control" />
                </div>
                <div class="form-group">
                    <label asp-for="ItemTitle" class="control-label"></label>
                    <input asp-for="ItemTitle" class="form-control" />
                </div>
                
    
            </div>
        </div>
        <div>
            @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
            <a asp-action="Index">Back to List</a>
        </div>
    

    Wednesday, July 10, 2019 3:10 PM

Answers

  • User475983607 posted

    If I do this does the code in my view stay the same or will that have to be updated?

    Change the view's model to MyViewModel or whatever you name the model.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2019 3:32 PM

All replies

  • User475983607 posted

    Wrap the two types in another class.

    public class MyViewModel 
    {
    	public Response response {get; set;}
    	public Item item {get; set;}
    }

    Wednesday, July 10, 2019 3:19 PM
  • User-501297529 posted

    Wrap the two types in another class.

    public class MyViewModel 
    {
    	public Response response {get; set;}
    	public Item item {get; set;}
    }

    If I do this does the code in my view stay the same or will that have to be updated?

    Wednesday, July 10, 2019 3:29 PM
  • User475983607 posted

    If I do this does the code in my view stay the same or will that have to be updated?

    Change the view's model to MyViewModel or whatever you name the model.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2019 3:32 PM
  • User-501297529 posted

    bootzilla

    If I do this does the code in my view stay the same or will that have to be updated?

    Change the view's model to MyViewModel or whatever you name the model.

    Thanks I believe this is it.

    Wednesday, July 10, 2019 3:41 PM