代码之家  ›  专栏  ›  技术社区  ›  Ayo

强类型局部视图MVC RC1

  •  0
  • Ayo  · 技术社区  · 15 年前

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

    视图页

    <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);
            }
    
    6 回复  |  直到 15 年前
        1
  •  2
  •   liammclennan    15 年前

    在评论中,您说过视图不是强类型的。因此:

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

    不会起作用。如果您强烈地将视图键入testmvcproject.models.information,然后从构造函数中传递该类型的实例,它将起作用。

    控制器:

    public ActionResult ShowAView()
    {
        Return View("WhateverYourViewIsCalled", new information());
    }
    
        2
  •  1
  •   alexl    15 年前

    您对html.renderpartial helper的使用有误解。 使用renderpartial时,您将显示视图,而不需要从控制器请求模型。

    因此,您必须重构视图页并将好的模型传递给用户控件:

    例:

    控制器:

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

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

    希望这有帮助。

        3
  •  0
  •   eulerfx    15 年前

    您是否也尝试将视图页设为通用视图页?

        4
  •  0
  •   Justin    15 年前

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

        5
  •  0
  •   Adam    12 年前

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

    ...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();
            }
        }
    
        6
  •  -1
  •   antonioh    15 年前

    我认为问题可能是您在表单中缺少名为“id”的元素,所以action方法的参数从未用值填充过?

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

    只是我的猜测…