0

将 ViewData.Model 传递给部分视图时遇到问题。即使我将其等同于结果查询,它也始终默认为 null。我无法访问强类型数据,因为模型为空。我目前的代码是这样的,

查看页面

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("header", this.ViewData.Model); %>
    <% Html.RenderPartial("test", this.ViewData.Model); %>
    <div id="userControls">
    </div>
</asp:Content>

用户控件 - 标题

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<testMVCProject.Models.information>" %>
<h2>
    ACReport</h2>
<p>
    id:
    <%= Html.Encode(Model.id) %>
</p>
<p>
    type:
    <%= Html.Encode(Model.type) %>
</p>

用户控制 - 测试

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<testMVCProject.Models.information>" %>

        <%  using (Ajax.BeginForm(
            "pressureV2",
            "Home",
            new { id = ViewData.Model.id },
            new AjaxOptions
            {
                UpdateTargetId = "userControls",
                HttpMethod = "GET"

            },
            new { @id = "genInfoLinkForm" }))
            {%>
        <%= Html.SubmitButton("hey", "Lol") %>

    <%} %>

控制器

public ActionResult header(int id)
        {
            var headerResults = from c in db.information
                                where c.id == id
                                select new information
                                {
                                    id = c.id,
                                    type = c.type
                                };
            ViewData.Model = headerResults.FirstOrDefault();
            return View(ViewData.Model);
        }

public ActionResult pressureV2(int id)
        {
            var pressureVResults = from c in db.pressure_volume_tests
                                   where c.id == id
                                   select new pressureVT
                                   {
                                       bottomCVP = c.bottom_CVP,
                                       topCVP = c.top_CVP
                                   };

            ViewData.Model = pressureVResults.FirstOrDefault();
            return View(ViewData.Model);
        }
4

6 回答 6

2

在您所说的评论中,该视图不是强类型的。正因为如此:

<% Html.RenderPartial("header", this.ViewData.Model); %>
<% Html.RenderPartial("test", this.ViewData.Model); %>

不管用。如果您将视图强输入到 testMVCProject.Models.information,然后从您的构造函数传递该类型的实例,它将起作用。

控制器:

public ActionResult ShowAView()
{
    Return View("WhateverYourViewIsCalled", new information());
}
于 2009-03-12T21:32:08.307 回答
1

您对 Html.RenderPartial 助手的使用有一个误解。当您使用 RenderPartial 时,您将显示视图而无需从控制器请求模型。

所以你必须重构你的 ViewPage 并将好的模型传递给你的用户控件:

示例:

控制器:

ActionResult MainView()
{
    var mainviewobj = new MainViewObject();

    var headerResults = from c in db.information
                                where c.id == id
                                select new information
                                {
                                    id = c.id,
                                    type = c.type
                                };

    mainviewobj.info = headerResults.FirstOrDefault();

    return view(mainviewobj);   
}

查看代码:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("header", this.ViewData.Model.info); %>
    <% Html.RenderPartial("test", this.ViewData.Model.info); %>
    <div id="userControls">
    </div>
</asp:Content>

查看后面的代码

public partial class MainView : ViewPage<MainViewObject>
{
}

现在模型在您的用户控件中不会为空。但请记住,用户控件渲染部分不需要执行控制器中的代码所以你不需要public ActionResult header(int id)在你的控制器中

希望这可以帮助。

于 2009-03-12T22:00:29.533 回答
0

您是否也尝试过使 ViewPage 通用?

于 2009-03-12T00:18:41.793 回答
0

RenderPartial 时不会调用 Controller - 它被绕过并直接呈现视图。因此,无论您想作为模型传入什么,都需要从调用视图中完成。

于 2009-03-12T21:40:45.507 回答
0

我发现这对我有用,像这样引用部分内容。

...form
    @Html.Partial("_AboutYou", Model.AboutYou);
 ..end form

在顶部的局部视图中......

@model <namespace1>.<namespace2>.<namespace3>.CustomerInfo.AboutYou
    @{

        ViewData.TemplateInfo.HtmlFieldPrefix = "AboutYou";

        if (this.ViewContext.FormContext == null)
        {
            this.ViewContext.FormContext = new FormContext();
        }
    }
于 2012-04-23T20:45:09.733 回答
-1

我相信问题可能是您在表单中缺少名称为“id”的元素,因此 Action 方法的参数永远不会填充值?

这样,查询将始终使用 FirstOrDefault 返回 null,因此返回 null 模型。

只是我的猜测...

于 2009-03-12T00:04:59.800 回答