【问题标题】:error when using partial render使用部分渲染时出错
【发布时间】:2015-06-13 23:11:07
【问题描述】:

你好我一直在尝试使用部分渲染从主视图访问我的部分视图的视图模型

局部视图是一个对话框,当我点击一个按钮时,对话框被打开,我通过调用控制器来加载它这里是控制器的代码:

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");}

但是我收到一个错误提示 reference undefined

你知道它可能是什么吗?

编辑:

viewdata[""]没关系,我已经删除了,问题就在这里

@foreach(var image in (IEnumerable)ViewBag.Images)

但是为什么这会导致问题

【问题讨论】:

  • 您在代码中维护ViewData["imageVenteCatalog"] 的位置?
  • 我已经编辑了我的帖子,谢谢
  • 我认为你需要 Html.RenderAction 以便调用该操作并返回 ``_ImagesPartial` 视图`

标签: c# jquery asp.net asp.net-mvc


【解决方案1】:

Html.RenderPartial 不调用操作,它只是呈现视图,并且您希望调用操作以便填充 ViewBag.Images,我怀疑您想要 Html.RenderAction

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

【讨论】:

  • 这似乎是合法的,我现在有了单选按钮的值,我的模型填充了它的值,如果我想在对话框上显示部分视图,请选择一个单选按钮,如何我可以保留我的模型吗?
  • 没错,结合我的主视图和弹出窗口的视图模型
【解决方案2】:

对于填充模型,您必须按以下方式使用:

@Html.Partial("_ImagesPartial", Model)

在你看来

在你的控制器中改变

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多