简体   繁体   English

使用部分渲染时出错

[英]error when using partial render

hello I have been trying to use partial render to access the view model of my partial view from the main view 你好,我一直在尝试使用局部渲染从主视图访问局部视图的视图模型

The partial view is a dialog , when I click a button, the dialog is opened and I load it by calling controller here is the code of the controller : 局部视图是一个对话框,当我单击一个按钮时,将打开该对话框并通过调用controller来加载它,这是该控制器的代码:

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

the In my partial view I use this code to show the images and a radio button 在我的局部视图中,我使用此代码显示图像和单选按钮

@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'/>
 }

finally in my main view I use renderpartial to get which radiobutton has been selected 最终在主视图中,我使用renderpartial来获取已选择哪个单选按钮

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

But instead I'm getting an error saying reference undefined 但是相反,我收到一条错误消息,指出引用未定义

any idea of what it could be ? 有什么想法吗?

EDIT: 编辑:

It doesn't matter for viewdata[""] , I have deleted it , the problem is here 对于viewdata [“”]都没有关系,我已将其删除,问题就在这里

@foreach(var image in (IEnumerable)ViewBag.Images) @foreach((IEnumerable)ViewBag.Images中的可变图像)

But why would this cause a problem 但是为什么这会引起问题

Html.RenderPartial不调用动作,它仅呈现View,并且您希望调用该动作,以便填充ViewBag.Images ,我怀疑您希望使用Html.RenderAction来代替:

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

For populated with model you have to use as per below : 对于填充模型,您必须按照以下说明使用:

@Html.Partial("_ImagesPartial", Model)

In your view 在您看来

And Change in your Controller 并改变您的控制器

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