强类型部分视图 MVC RC1

发布于 2024-07-14 22:28:52 字数 2433 浏览 5 评论 0原文

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

ViewPage

<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>

UserControl - header

<%@ 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>

UserControl - test

<%@ 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") %>

    <%} %>

Controller

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

having a problem passing ViewData.Model to the partial views. It always is defaulting to null even if I equate it to a result query. I cannot access the strongly typed data because the Model is null. My current code is this,

ViewPage

<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>

UserControl - header

<%@ 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>

UserControl - test

<%@ 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") %>

    <%} %>

Controller

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最美的太阳 2024-07-21 22:28:53

在评论中您说过该视图不是强类型的。 因为这样:

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

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

控制器:

public ActionResult ShowAView()
{
    Return View("WhateverYourViewIsCalled", new information());
}

In the comments you have said that the view is not strongly typed. Because of that:

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

will not work. If you strongly type your view to testMVCProject.Models.information and then pass an instance of that type from your constructor it will work.

Controller:

public ActionResult ShowAView()
{
    Return View("WhateverYourViewIsCalled", new information());
}
自找没趣 2024-07-21 22:28:53

您对 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)

希望这会有所帮助。

You have a misunderstanding of the use of Html.RenderPartial helper.
When you use the RenderPartial you will show the view without requesting the model from the controller.

So you have to refactor your ViewPage and pass the good Model to your usercontrols:

Exemple:

Controller:

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

View Code:

<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>

View Code Behind

public partial class MainView : ViewPage<MainViewObject>
{
}

Now the Model will not be null in your usercontrol.
But remember the usercontrol rendering partially dun execute the code in the controller
So you dun need the public ActionResult header(int id) in your Controller

Hope this helps.

清君侧 2024-07-21 22:28:53

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

Have you tried making the ViewPage generic as well?

溺渁∝ 2024-07-21 22:28:53

当您 RenderPartial 时,控制器不会被调用 - 它会被绕过并直接渲染视图。 因此,无论您想要作为模型传递什么,都需要从调用视图中完成。

The Controller doesn't get called when you RenderPartial - it is bypassed and the view is rendered directly. So whatever you want to pass in as a model needs to be done from the calling View.

你怎么敢 2024-07-21 22:28:53

我发现这对我有用,像你一样参考部分,就像这样。

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

I found this worked for me, reference the partial as you do, like so.

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

within the partial view at the top...

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

        ViewData.TemplateInfo.HtmlFieldPrefix = "AboutYou";

        if (this.ViewContext.FormContext == null)
        {
            this.ViewContext.FormContext = new FormContext();
        }
    }
一影成城 2024-07-21 22:28:53

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

这样,查询将始终返回带有 FirstOrDefault 的 null,因此模型为 null。

只是我的猜测...

I believe the problem might be that you're missing an element in the form with the name "id" so the parameter of the Action method is never populated with a value?

That way the query would always return null with the FirstOrDefault, hence the null Model.

Just my guess...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文