我可以拥有使用两个模型的部分视图吗?

发布于 2024-11-28 07:19:06 字数 1406 浏览 1 评论 0原文

我正在尝试制作如下部分视图:

  • 当用户未登录时,显示注册表单。

  • 当用户登录时,显示该用户信息。

我有一个局部视图,既可以作为注册表,又可以作为用户信息卡;取决于用户是否登录。

由于该部分视图必须从网站中的任何位置都可见,因此我将其编写在 MVC3 应用程序的 _layout 区域中:

<body>
    <div id="wrapper">

        <div id="top">
            <p id="onlinecount">7890 Personas Jugando</p>
            <p id="logoncontrol">ENTRAR | REGISTRARSE</p>
        </div>

        <div id="headercontainer">
            <img src="../../Content/Images/topheadertemp.png" />
        </div>

        <div id="middle">
            <div id="playerinformation">
                @Html.Partial("_RegisterPartial") <!-- HERE! -->
            </div>            

            <div id="centerad">
                <img src="../../Content/Images/premio.png" />
            </div>

            <div id="rightad">
                <img src="../../Content/Images/ganadores.png" />
            </div>

            <div class="clear"></div>
        </div>

        <div id="bottom">@RenderBody()
        </div>

    </div>
</body>

基本上,我需要在该部分中显示一个表单,如果用户未登录。但是,如果他登录了(通过 cookie 或其他方式),我应该加载他的信息模型,以便显示他帐户的数据。

这就是我被困住的地方。我不知道如何加载此用途的模型。由于这是在 _layout 中,如果我是正确的,则没有控制器对其进行操作,不是吗?

有什么建议吗?

I'm trying to make a partial view that acts as the following:

  • When user is not logged on, show a registration form.

  • When user is logged on, show me that users information.

I have a partial view that acts as both as registration form, and a user information card; depending on whether or not the user is logged in.

Since this partial view has to be visible from anywhere in the website, I wrote it in the _layout area of the MVC3 application:

<body>
    <div id="wrapper">

        <div id="top">
            <p id="onlinecount">7890 Personas Jugando</p>
            <p id="logoncontrol">ENTRAR | REGISTRARSE</p>
        </div>

        <div id="headercontainer">
            <img src="../../Content/Images/topheadertemp.png" />
        </div>

        <div id="middle">
            <div id="playerinformation">
                @Html.Partial("_RegisterPartial") <!-- HERE! -->
            </div>            

            <div id="centerad">
                <img src="../../Content/Images/premio.png" />
            </div>

            <div id="rightad">
                <img src="../../Content/Images/ganadores.png" />
            </div>

            <div class="clear"></div>
        </div>

        <div id="bottom">@RenderBody()
        </div>

    </div>
</body>

Basically, I need to show a form in that partial if the user is not logged on. However if he IS logged on (via cookie or whatever), I should load a model of his information in order to display the data for his account.

Here's where I'm stuck. I don't know how to load the model for this usage. Since this is in the _layout, no controller acts on it if I'm correct, no?

Any suggestions?

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

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

发布评论

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

评论(4

〆凄凉。 2024-12-05 07:19:06

您应该查看之前的问题,这和你的很相似。

您的页面应该在单个模型中包含注册和用户信息。 (所以你的问题的答案是你的页面只有一个模型。但是你的模型可以由其他对象组成——每个部分视图一个。)

所以正如你在链接中看到的,用户的部分视图仅使用页面模型中与其相关的那些对象。

我想这应该对你有帮助。希望这有帮助!祝你好运。

更新:抱歉延迟了,但这里有一个可能有帮助的示例(大量代码):

模型
我创建了一个抽象视图模型,其中始终包含注册表和用户数据。每个页面的模型都可以继承这个摘要。

public class Registration
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

public class UserData
{
    public string DisplayName { get; set; }
    public int Age { get; set; }
}

public abstract class RegModelViewModelBase
{
    public string Title { get; set; }
    public Registration RegInfo { get; set; }
    public UserData UserInfo { get; set; }
}

public class MainPageViewModel : RegModelViewModelBase
{
}

控制器
在这里,我只是实例化该页面/视图的具体视图模型(MainPageViewModel)。我设置属性(可能来自数据库等)。我将视图模型传递给视图。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        MainPageViewModel hpvm = new MainPageViewModel();

        hpvm.Title = "Some Cool Page";
        hpvm.RegInfo = new Registration() { Password = "blah", UserName = "dhoerster" };
        hpvm.UserInfo = new UserData() { DisplayName = "David Hoerster", Age = 125 };

        return View(hpvm);
    }
}

视图 -- _Layout.cshtml:
请注意,模型中的第一行为我的 _layout 模板设置模型对象。我从控制器获取此视图模型,并且可以在 _layout (或其他模板)中引用它。我在这里没有做太多事情,除了获取部分视图(_RegStuff)并将我的模型(在控制器中设置)的 RegInfo 传递给它:

@model MvcApplication1.Models.RegModelViewModelBase

<!DOCTYPE html>
<html>
<head>
    <title>@Model.Title</title>
</head>

<body>
    @Html.Partial("_RegStuff", Model.RegInfo)
    @RenderBody()
</body>
</html>

View -- _RegInfo.cshtml:
很简单,但我再次设置了此部分视图期望传入的模型类型。

@model MvcApplication1.Models.Registration
<div>User Name = @Model.UserName</div>

View -- Index.cshtml:
再次设置模型并在我的索引视图中使用它。

@model MvcApplication1.Models.MainPageViewModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Title</h2>
<h3>Display Name = @Model.UserInfo.DisplayName</h3>

因此,在整个过程中,我可以引用控制器中的模型集。

我希望这能解释我想要表达的意思。如果没有,我可以相应地更新。

谢谢!

You should look at this previous question, which is similar to yours.

You should have registration and user information in a single model for your page. (So the answer to your question is that your page only has one model. But your model can be made up of other objects -- one for each partial view.)

So as you see in the link, the user had partial views only use those objects in the page model that pertained to it.

I think this should help you out. Hope this helps! Good luck.

UPDATE: Sorry for the delay, but here's an example (lots of code) that may help:

Model:
I create an abstract view model that always has reg and user data in it. Every page's model could inherit from this abstract.

public class Registration
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

public class UserData
{
    public string DisplayName { get; set; }
    public int Age { get; set; }
}

public abstract class RegModelViewModelBase
{
    public string Title { get; set; }
    public Registration RegInfo { get; set; }
    public UserData UserInfo { get; set; }
}

public class MainPageViewModel : RegModelViewModelBase
{
}

Controller:
Here, I just instantiate the concrete view model for this page/view (MainPageViewModel). I set properties (which could come from the database, etc.). I pass the view model to the view.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        MainPageViewModel hpvm = new MainPageViewModel();

        hpvm.Title = "Some Cool Page";
        hpvm.RegInfo = new Registration() { Password = "blah", UserName = "dhoerster" };
        hpvm.UserInfo = new UserData() { DisplayName = "David Hoerster", Age = 125 };

        return View(hpvm);
    }
}

View -- _Layout.cshtml:
Notice that the first line in my model sets the model object for my _layout template. I'm getting this view model from the controller, and I can reference it in _layout (or other template). I don't do much here, except get a partial view (_RegStuff) and pass to it my RegInfo from my model (which was set in the controller):

@model MvcApplication1.Models.RegModelViewModelBase

<!DOCTYPE html>
<html>
<head>
    <title>@Model.Title</title>
</head>

<body>
    @Html.Partial("_RegStuff", Model.RegInfo)
    @RenderBody()
</body>
</html>

View -- _RegInfo.cshtml:
Dirt simple, but again I set my model type that this partial view expects to be passed in.

@model MvcApplication1.Models.Registration
<div>User Name = @Model.UserName</div>

View -- Index.cshtml:
Again, set the model and use it in my index view.

@model MvcApplication1.Models.MainPageViewModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Title</h2>
<h3>Display Name = @Model.UserInfo.DisplayName</h3>

So throughout, I can reference the model set in my controller.

I hope this explains what I was trying to get at. If not, I can update this accordingly.

Thanks!

随梦而飞# 2024-12-05 07:19:06

实现此目的的一种方法是设计一个如下所示的 ViewModel:

public class ViewModel
{
    public PlayerCard playerCard { get; set; }
    public RegisterModel registerModel { get; set; }
}

包含两个模型。

One way you could do this is design a ViewModel like this:

public class ViewModel
{
    public PlayerCard playerCard { get; set; }
    public RegisterModel registerModel { get; set; }
}

To include both models.

哭泣的笑容 2024-12-05 07:19:06

您可以创建一个新类“ViewModel”来保存这两个模型,
或者您可以将两个或其中一个模型放入 ViewBag 中

You can create a new class "ViewModel" hold both the models,
Or you can put both or one of the Models in the ViewBag

空城旧梦 2024-12-05 07:19:06

我将在这里违背常规,并建议您不要在视图中做出此决定。有两个独立的视图,具有自己的模型,并让控制器根据用户是否登录(经过身份验证)来确定调用哪个视图。

这并不是说您正在考虑的方法无法完成...您可以通过创建一个由您需要的每个视图模型组成的 ViewModel 来轻松地做到这一点。但我仍然建议不要这样做。

I'm going to go against the grain here, and recommend that you don't make this decision in the view. Have two separate views, with their own models, and let the controller determine which view is called, based on whether the user is logged in (authenticated) or not.

It's not that the approach that you're looking at can't be done... you can easily do this by making a ViewModel that is composed of each of the view models that you need. I'd still advise against it though.

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