Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
@model IEnumerable <bartering.models.advertisement>
@model Bartering.Models.Advertisement



I have a view called index.cshtml I want to get all the attributes of the two models to my view.but I can't add two models to same view.How can I do this without adding two models????
I have a model advertisement.cs.


@model Bartering.Models.Advertisement


@{
ViewBag.Title = "Index";
}



Create a Task

@{var date = DateTime.Now;

}

Today is @date




    Tasks are shown below



    <html style="width:100%">
    <body style="height:100%">




    User Name Statusn Completed Tasks

    @foreach (var advertisement in Model){


    • @Html.ActionLink(advertisement.Task_For, "Details", advertisement)


    }
    @{ //
    • @Html.ActionLink(advertisement.Task_For,"Details",advertisement)
    // All users assigned tasks are shown here
    var db = new Bartering.Models.AdvertisementDataContext();

    // var selectdata = db.Query("SELECT Task_For,Count(*) FROM Advertisements GROUP BY Task_For");
    // var grid = new WebGrid(source: selectdata);

    var Query1 = db.Advertisements.GroupBy(Advertisements => Advertisements.Task_For);

    foreach (var advertisement in Query1)

    {
    //
    • @Html.ActionLink(@advertisement.Key, "Details", advertisement)




    • @advertisement.Key @advertisement.Count()

    }


    }

    </body>
    </html>






    this is my model




    using System;
    using System.Drawing; // Image type is in this namespace
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;

    namespace Bartering.Models
    {
    public class Advertisement : IEnumerable<advertisement>
    {
    //public IEnumerable<bartering.models.advertisement> abc{get;set;}
    [Key]
    public int ID { get; set; }

    [Required]
    [StringLength(100)]
    public string Task_For { get; set; }

    public Guid OwnerID { get; set; }

    public string UserName { get; set; }

    [Required]
    public string Department { get; set; }


    public byte[] Attachment { get; set; }

    [Required]
    [StringLength(200)]
    public string Description { get; set; }

    public int Count { get; set; }

    [Required(ErrorMessage = "Date is required")]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime Date{ get; set; }



    public IEnumerator<advertisement> GetEnumerator()
    {
    throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
    throw new NotImplementedException();
    }





    }
    }
Posted
Updated 16-Jan-14 5:12am
v6
Comments
bowlturner 16-Jan-14 10:08am    
Sorry not a clue what you are asking to do. try 'Improve question'?
bowlturner 16-Jan-14 10:18am    
Ok, can you add some of the code you are trying to use?
Sergey Alexandrovich Kryukov 16-Jan-14 10:39am    
You have been advised: try to improve question. Please do just that.
—SA

1 solution

You don’t call two models on a single view but you have some alternate to perform this operation such like that:
1. Use Other view that binds with other model and call with help of @Html.Action() method.

C#
@Html.Action("ActionName", "ControllerName",new { id = Model.Id })


2. Use Other partial view that populate with other model and call with help of @Html.Partial() or @Html.EditiorFor<>().

C#
@Html.Partial("_partialView", Model)


3. Define property of sub model type in base model which binds and access value using base model for example, define to model which binds on single view
C#
public class EmployeeModel
{
    public EmployeeModel()
    {
        Address = new AddressModel();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public AddressModel Address{get;set;}
}
public class AddressModel
{
    public string City { get; set; }
    public string Country { get; set; }
}


Define a view to use these models
HTML
@model EmployeeModel
@* from base model *@
@Html.TextBoxFor(model=>model.Name)
@* from child model *@
@Html.TextBoxFor(model => model.Address.City)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900