1

现在我敢打赌,这是一个经常发生的问题,我似乎找不到任何与之相关的东西,或者注意到自己有什么问题。底部提供的视图始终foreach在线返回 null。

我正在制作一个简单的货币列表。

模型:

public class Currency
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Sign { get; set; }
    public float BuyValue { get; set; }
    public float MidValue { get; set; }
    public float SellValue { get; set; }

    public string CurrencyIcon { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime DateCreated { get; set; }
}

它应该以几种不同的方式发布为部分视图。例如,将获得所有这些的部分视图(今天),以及将获得随机 6 的部分视图(今天也是)。

我现在正在做第一部分,所有这些,今天。

控制器

public ActionResult FrontPagePartial()
{
    DateTime today = DateTime.Now.Date; //i'm filtering it by date only
    var currencies = db.Currencies.Where(c => c.DateCreated.Equals(today));

    return View(currencies);
}

我已经检查过它是否正常工作的过滤。所以我的观点是这样的:

@model IEnumerable<CurrencyList.Models.Currency>

<div id="currencyList_Partial">
    <div id="bg_currencyList_Partial">
        <img src="@Url.Content("~/Content/Resources/img.png")" />
    </div>
    <div id="header_currencyList_Partial">
        <div style="padding: 10px;">
            <p style="font-size: 18px;">
                Currency List<br />
                <span class="header_currencyList_Partial_Day">FOR TODAY</span>
            </p>
            <p class="header_currencyList_Partial_Link" >
                <a href="#">DETAILS ></a>
            </p>
        </div>
        <div id="table_currencyList">
            @foreach (var item in Model)
            { 
                <table>
                    <tr>
                        <td style="text-align: center;"><img src='@item.CurrencyIcon' title='@item.Sign' /><p style="font-size: small;">@Html.DisplayFor(modelItem => item.Name)</p><p>@Html.DisplayFor(modelItem => item.Sign)</p></td>
                        <td style="text-align: center;">@Html.DisplayFor(modelItem => item.BuyValue)</td>
                        <td style="text-align: center;">@Html.DisplayFor(modelItem => item.MidValue)</td>
                        <td style="text-align: center;">@Html.DisplayFor(modelItem => item.SellValue)</td>
                    </tr>
                </table>
            }
        </div>
    </div>
</div>

父视图

@{
    ViewBag.Title = "Currency List Home Page";
}

@section HeadSection{
    <link rel="stylesheet" href="@Url.Content("~/Content/layerslider.css")" type="text/css" media="screen" />
    <link rel="stylesheet" href="@Url.Content("~/Content/defaultskin/skin.css")" type="text/css" media="screen" />
    <noscript>
        <link rel="stylesheet" href="@Url.Content("~/Content/noscript.css")" type="text/css" media="screen" />
    </noscript>
}

@section ScriptSection{
    <script src="@Url.Content("~/Scripts/jqueryui.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/plugins.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/page.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/layerslider.kreaturamedia.jquery.js")" type="text/javascript"></script>
}
<div id="content">
    <div id="statusbar"></div>
    <div id="slider">
        <div id="layerslider">
            <div class="ls-layer" style="slidedelay: 3000">
                <img class="ls-bg" src="@Url.Content("~/Content/images/slider_gallery/slide1_layer1.png")" alt="layer" />
                <img class="ls-s2" src="@Url.Content("~/Content/images/slider_gallery/behind_text_layer.png")" alt="sublayer" style="durationin: 0; top: 247px;/*easingin: easeOutElastic*/" />
                <img class="ls-s3" src="@Url.Content("~/Content/images/slider_gallery/text_layer1.png")" alt="sublayer" style="durationin: 2000; top: 255px;left: 15px; easingin: easeOutElastic " />
            </div>
            <div class="ls-layer" style="slidedelay: 3000">
                <img class="ls-bg" src="@Url.Content("~/Content/images/slider_gallery/slide2_layer1.png")" alt="layer" />
                <img class="ls-s2" src="@Url.Content("~/Content/images/slider_gallery/behind_text_layer.png")" alt="sublayer" style="durationin: 0; top: 247px;/*easingin: easeOutElastic*/" />
                <img class="ls-s3" src="@Url.Content("~/Content/images/slider_gallery/text_layer1.png")" alt="sublayer" style="durationin: 2000; top: 255px;left: 15px;easingin: easeOutElastic " />
            </div>
            <div class="ls-layer" style="slidedelay: 3000">
                <img class="ls-bg" src="@Url.Content("~/Content/images/slider_gallery/slide3_layer1.png")" alt="layer" />
                <img class="ls-s2" src="@Url.Content("~/Content/images/slider_gallery/behind_text_layer.png")" alt="sublayer" style="durationin: 0;top: 247px;" />
                <img class="ls-s3" src="@Url.Content("~/Content/images/slider_gallery/text_layer1.png")" alt="sublayer" style="durationin: 2000; top: 255px; left: 15px; easingin: easeOutElastic " />
            </div>
        </div>
    </div>
    <div id="converter">
        <h2>CONVERTER</h2>
        <p class="shadowedText">
            VALUE:
        </p>
        <p>
            <input type="text" id="tb11" class="tb11" />
        </p>
        <p class="shadowedText">
            FROM CURRENCY
        </p>
        <div class="select1 select1p">
                <select id="select1">
                    <option>EUR - EURO</option>
                    <option> $  - DOLLAR</option>
                </select>
            <span class="selectArrow"><img src="@Url.Content("~/Content/images/select_arrows.png")" /></span>
        </div>
        <p class="shadowedText">
            TO CURRENCY
        </p>
        <div class="select1 select1p">
                <select id="select2">
                    <option> $ - EURO</option>
                    <option> EURO  - DOLLAR</option>
                </select>
            <span class="selectArrows"><img src="@Url.Content("~/Content/images/select_arrows.png")" /></span>
        </div>
        <div class="select1p addMargin">
            <span class="boldResult">100 EUR = 11710.55 $</span><br />
            <span class="lightResult">100 $ = 0.84882 EUR</span>
        </div>
    </div>

</div>

<div id="lowerContent">
    <div id="blog"><img src="@Url.Content("~/Content/images/vesti.png")" /></div>


    //This is where Partial View is rendered
    <div id="listC">@Html.Partial("FrontPagePartial")</div>


</div>

<div id="portals">

</div>
4

2 回答 2

4

<div id="listC">@Html.Partial("FrontPagePartial")</div>

这是尝试使用未定义的父视图模型来呈现局部视图 FrontPagePartial。

public ActionResult FrontPagePartial()

这将创建一个操作,以便您可以执行 siteurl/controller/FrontPagePartial


你需要做的是

<div id="listC">@Html.Partial("FrontPagePartial", currencies)</div>

如果你不介意 ViewBag,你可以在 Action 方法中设置 ViewBag.Currencies 和

<div id="listC">@Html.Partial("FrontPagePartial", ViewBag.Currencies)</div>

于 2012-10-14T03:04:11.000 回答
1

@Html.Partial 只是使用父级拥有的模型直接从文件视图(在 Shared 文件夹中,而不是通过控制器中的 FrontPagePartial 方法)呈现 html 部分。在您的情况下,Razor 尝试使用父视图中的模型,但显然您从未定义过它。这就是空对象的原因

@foreach (var item in Model) { ... } 

您想调用返回另一种对象模型的局部视图;@Httml.Partial 和 @Html.RenderPartial 都无法做到这一点。两种解决方案:

阿贾克斯调用:

$(document).ready(function() { 
    $.get('@Url.Action("FrontPagePartial")',null,function(data) {
        $('#listC').html(data);
    });
});

使用 Html.RenderAction 或 Html.Action:

<div id="listC">@Html.RenderAction("FrontPagePartial")</div>

不要忘记更改 FrontPagePartial 方法中的返回值。如果没有这个,结果将是布局页面内的货币列表(您返回的是一个简单的页面,而不是部分视图):

public ActionResult FrontPagePartial()
{
....
return PartialView(currencies);
}
于 2012-10-14T07:33:16.603 回答