簡體   English   中英

使用部分渲染時出錯

[英]error when using partial render

你好,我一直在嘗試使用局部渲染從主視圖訪問局部視圖的視圖模型

局部視圖是一個對話框,當我單擊一個按鈕時,將打開該對話框並通過調用controller來加載它,這是該控制器的代碼:

public ActionResult ImagesPartial()
{
    ViewBag.Images = Directory.EnumerateFiles(Server.MapPath("~/images_upload"))
                      .Select(fn => "~/images_upload/" + Path.GetFileName(fn));
    return PartialView("_ImagesPartial");
}

在我的局部視圖中,我使用此代碼顯示圖像和單選按鈕

@foreach(var image in (IEnumerable<string>)ViewBag.Images)
{
    @Html.RadioButtonFor(model => model.imageVenteCatalog, image)
    <img src="@Url.Content(image)" alt="Hejsan" width="100" height="100" class='selectable-image'/>
 }

最終在主視圖中,我使用renderpartial來獲取已選擇哪個單選按鈕

@{Html.RenderPartial("_ImagesPartial");}

但是相反,我收到一條錯誤消息,指出引用未定義

有什么想法嗎?

編輯:

對於viewdata [“”]都沒有關系,我已將其刪除,問題就在這里

@foreach((IEnumerable)ViewBag.Images中的可變圖像)

但是為什么這會引起問題

Html.RenderPartial不調用動作,它僅呈現View,並且您希望調用該動作,以便填充ViewBag.Images ,我懷疑您希望使用Html.RenderAction來代替:

@{ Html.RenderAction("ImagesPartial","ControllerName"); }

對於填充模型,您必須按照以下說明使用:

@Html.Partial("_ImagesPartial", Model)

在您看來

並改變您的控制器

public ActionResult ImagesPartial()
{
    //strore data in your model class data
    return PartialView("_ImagesPartial", pass your model data);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM