如何发送以查看DTO和模型

时间:2015-12-23 13:37:22

标签: asp.net-mvc

我的Dto

public class homePageDTO
{
    public IEnumerable<Yazi> yazi { get; set; }
    public IEnumerable<Yorum> yorum { get; set; }
}

我的ActionMethod

public ActionResult Post(int pid)
{
    homePageDTO obj = new homePageDTO();
    obj.yazi = ent.Yazi.Where(x=>(x.YaziID==(int)pid)).ToList();
    obj.yorum = ent.Yorum.Where(x => (x.YorumNo == (int)pid)).ToList();

    return View(obj);
}

我的多模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication15.Models
{
    public class BigViewModel
    {
        public homePageDTO home { get; set; }
        public Yorum Yorrum { get; set; }
    }
}

我的观点

  @model WebApplication15.Models.BigViewModel
@{
    ViewBag.Title = "Post";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var item in Model.home.yazi)
{
    <header class="intro-header" style="background-image: url('/content/img/post-bg.jpg')">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <div class="site-heading"> 

                        <h1>@item.YaziUstBaslik</h1>
                        <hr class="small">
                        <span class="subheading"><b>@item.YaziAltBaslik</b></span>
                    </div>
                </div>
            </div>
        </div>
    </header>
    <!-- Post Content -->
    <article>
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <p>@item.Yazi1</p>
                    <p class="alert alert-warning">Yollayan @item.YazarKullaniciAdi-@item.YaziTarih</p>
                </div>
            </div>
        </div>
    </article>

    <hr>
}
<h2>Index</h2>
<table class="table">
    <tr>
        <th>
            Yorum
        </th>
        <th>
            Yorum Sahibi
        </th>
        <th>
            Tarihi
        </th>
        <th></th>
    </tr>

    @foreach (var x in Model.home.yorum)
    {
        <tr>
            <td>
                @x.Yorum1
            </td>
            <td>
                @x.YorumSahibi
            </td>
            <td>
                @x.YorumTarihi
            </td>
        </tr>
    }

</table>

<div class="container">
    <h2>Modal Example</h2>
    <!-- Trigger the modal with a button -->
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Yeni Yorum</button>
    <!-- Modal -->
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Selam</h4>
                </div>
                <div class="modal-body">
                    <p>nasdsadasdasda</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Yolla</button>
                </div>
            </div>

        </div>
    </div>

</div>

ERROR

传递到字典中的模型项的类型为“WebApplication15.Models.homePageDTO”,但此字典需要“WebApplication15.Models.BigViewModel”类型的模型项。

它被解散了

我的新动作方法

public ActionResult Post(int pid)
        {
            BigViewModel bigyorum = new BigViewModel();
            bigyorum.home = new homePageDTO();
            bigyorum.home.yazi = ent.Yazi.Where(x=>(x.YaziID==(int)pid)).ToList();
            bigyorum.home.yorum = ent.Yorum.Where(x => (x.YorumNo == (int)pid)).ToList();
            return View(bigyorum);
        }

1 个答案:

答案 0 :(得分:0)

您需要更改视图中相关的模型,在视图的第一行设置此项:

@model  WebApplication15.Models.homePageDTO

我认为homePageDTO位于名称空间WebApplication15.Models中,如果没有,则放入正确的名称空间。

随着你的更新:

在您的操作中,您传递的是homePageDTO类型的对象:

public ActionResult Post(int pid)
{
    homePageDTO obj = new homePageDTO();
    obj.yazi = ent.Yazi.Where(x=>(x.YaziID==(int)pid)).ToList();
    obj.yorum = ent.Yorum.Where(x => (x.YorumNo == (int)pid)).ToList();

    return View(obj);
}

并且在您看来,哟有:

@model WebApplication15.Models.BigViewModel

有些不同的对象,您从操作传递给视图的对象类型必须与您在视图中声明的对象类型相同。

因此,更改操作以返回 BigViewModel 或将视图更改为 homePageDTO