Search code examples
modelcontrollerasp.net-core-mvc

Sending two models with one controller


I haven't been able to fit the logic in the MVC structure yet. My problem is:

public IActionResult Index(string ilceler)
        {
          
            var ETList = c.ETs.Where(p => p.Ilce == ilceler && p.ETDurumu != "YOK").OrderBy(p=>p.KurumAdi).ToList();
            var ETListOlmayanlar = c.ETs.Where(p => p.Ilce == ilceler &&p.ETDurumu=="YOK").OrderBy(p => p.KurumAdi).ToList();

            return View();
        }

does not return two models and are not bind on the view side.

Learn asp.net core details


Solution

  • You can create a DTO model that contains the two models you want.

    I noticed that you are actually using the same model, but filtering the value of ETDurumu to create two sets of data. You can refer to my test code below:

    Original model:

    public class ETList
    {
        [Key]
        public int Id { get; set; }
        public string Ilce { get; set; }
        public string ETDurumu { get; set; }
        public int KurumAdi { get; set; }
    }
    

    DTO model:

    public class ETDTO
    {
        public List<ETList> eTLists1 { get; set; }
        public List<ETList> eTLists2 { get; set; }
    }
    

    Controller:

    public IActionResult Index(string ilceler)
    {
        ilceler = "test1";
        ETDTO eTDTO = new ETDTO();
        var ETList = c.ETs.Where(p => p.Ilce == ilceler && p.ETDurumu != "YOK").OrderBy(p => p.KurumAdi).ToList();
        var ETListOlmayanlar = c.ETs.Where(p => p.Ilce == ilceler && p.ETDurumu == "YOK").OrderBy(p => p.KurumAdi).ToList();
        eTDTO.eTLists1 = ETList;
        eTDTO.eTLists2 = ETListOlmayanlar;
    
        return View(eTDTO);
    }
    

    View(Use the DTO model):

    @model Project.Models.ETDTO
    
    <div>
        <table class="table">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Ilce</th>
                    <th>ETDurumu</th>
                    <th>KurumAdi</th>
                </tr>
            </thead>
            <tbody>
                @foreach(var item in Model.eTLists1)
                {
                    <tr>
                        <td>@item.Id</td>
                        <td>@item.Ilce</td>
                        <td>@item.ETDurumu</td>
                        <td>@item.KurumAdi</td>
                    </tr>    
                }
            </tbody>
        </table>
    </div>
    
    <hr />
    
    <div>
        <table class="table">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Ilce</th>
                    <th>ETDurumu</th>
                    <th>KurumAdi</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.eTLists2)
                {
                    <tr>
                        <td>@item.Id</td>
                        <td>@item.Ilce</td>
                        <td>@item.ETDurumu</td>
                        <td>@item.KurumAdi</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    

    Test Result:

    enter image description here

    Please let me know if this is what you want. Hope this can help you.