部分视图返回null模型

时间:2012-10-13 15:52:55

标签: c# asp.net-mvc razor

现在我打赌这是一个经常发生的问题,我似乎无法找到与之相关的任何内容,或者注意到自己有什么问题。底部提供的视图始终在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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

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

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

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

Ajax电话:

$(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);
}