0

私は私のプロジェクトブラウザページです。独自のコントローラ(単なるユーザーコントロールではない)を保証する2つのサブ要素があります-UploadとFileBrowserなので、Html.RenderAction(Action、Controller、param)を使用して追加しました。

ただし、問題は、ブラウズページにProjectViewModelが必要であり、UploadがUploadViewModelなどを使用することです。したがって、これらのHtml.RenderAction要素を使用すると、ブラウズページはProjectViewModelの受信をすぐに停止するように見えます。VMに切り替わると思います。最後のRenderActionの。

これらのすでに強く型付けされたビューがコンテキストを維持するように、ルーティングで設定する必要があるものはありますか?

コードで更新:

また、「アップロード」するモデルが別のものであることを明示的に述べる必要があるかもしれません。私は知らないよ。

ブラウザ(UploadとFileBrowserを含む):

<%@ 
Page Title="" 
Language="C#" 
Inherits="System.Web.Mvc.ViewPage<CKD.Web.Files.ViewModels.ProjectViewModel>"
MasterPageFile="~/Views/Project/Project.Master" 
%>

<asp:Content runat="server" ID="Main" ContentPlaceHolderID="MainContent">
    <table>
<tr>
<td id="upload" style="width: 180px" class="ui-widget ui-widget-content ui-corner-all">
    <% Html.RenderAction("Index", "Upload", new {id = Model.Project.Id}); %>
</td>
<td id="fileBrowser" style="width: auto" class="ui-widget ui-widget-content ui-corner-all">
    <% Html.RenderAction("Index", "FileBrowser", new {id = Model.Project.Id}); %>
</td>
</tr>
</table>
</asp:Content>

ビューのアップロード:

<%@ 
Page Title="" 
Language="C#" 
Inherits="System.Web.Mvc.ViewPage<CKD.Web.Files.ViewModels.UploadViewModel>" 
MasterPageFile="~/Views/Shared/Control.master"
%>
<%@ Import Namespace="System.IO" %>

<asp:Content runat="server" ID="Scripts" ContentPlaceHolderID="Scripts">
</asp:Content>

<asp:Content runat="server" ID="Main" ContentPlaceHolderID="MainContent">
<div class="uploadControl" style="Margin: 8px">
<h2 style="Margin-Bottom: 0px">Upload</h2>
<hr />

<div id="accordion" style="display: block;">
    <h3><a href="#">Files</a></h3>
    <div>
        <div class="ui-widget-content ui-corner-all" style="min-height: 80px; margin: 4px">
            <% if(Model.Files != null) %>
                <% foreach(FileInfo f in Model.Files) {%>
                    <p><%= f.Name %></p>
                    <hr />
                <% } %>
        </div>
        <ul style="width: 10px; list-style-type:none">
            <li class="ui-widget ui-widget-button ui-corners-all">Clear</li>
            <li class="ui-widget ui-widget-button ui-corners-all">Add</li>
        </ul>
    </div>
    <h3><a href="#">Transmittal</a></h3>
    <div>
        <p>File here</p>
        <p style="width: auto; margin: 8px" class="ui-widget-button">Pick File...</p>
    </div>
    <h3><a href="#">Notification</a></h3>
    <div>
        <p>
        Stuff
        </p>
    </div>
</div>
<div>
<div class="ui-widget ui-corner-all ui-widget-active">Upload Files</div>
</div>

</div>
</asp:Content>

コントローラーのアップロード:

using System.Web.Mvc;

namespace CKD.Web.Files.Controllers
{
    using System.Linq;
    using Models;
    using ViewModels;

    public class UploadController : Controller
    {
        private ICKDClientAreaRepository Repository { get; set; }
        private UploadViewModel _viewModel;

                        private UploadViewModel ViewModel
    {
        get { return _viewModel ?? (_viewModel = ViewModel = UploadViewModel.Default(Repository)); }
        set { _viewModel = value; }
    }

                    public UploadController(ICKDClientAreaRepository repository)
    {
        Repository = repository;
    }

        // GET
        public ActionResult Index(int id)
        {
            var project = Repository.Projects.Single(x => x.Id == id);
            ViewModel = UploadViewModel.ForProject(project, Repository);

            return View(ViewModel);
        }
    }
}

VMのアップロード:

namespace CKD.Web.Files.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web.Security;
    using Models;

    public class UploadViewModel
    {
        public Project Project { get; set; }
        public DirectoryInfo Directory { get; set; }
        public User Uploader { get; set; }
        public DateTime Time { get; set; }

        public List<FileInfo> Files { get; set; }
        public FileInfo Transmittal { get; set; }

        public List<User> NotificationList { get; set; }

                        public static UploadViewModel Default(ICKDClientAreaRepository fromRepository)
    {
        var project = fromRepository.Projects.First();

        return ForProject(project, fromRepository);
    }

                                                                public static UploadViewModel ForProject(Project project, ICKDClientAreaRepository fromRepository)
    {
        var dir = project.DirectoryName;
        var uploader = fromRepository.Users.Single(x => x.Username == Membership.GetUser().UserName);
        var time = DateTime.Now;
        var notification = project.Users.ToList();

        return new UploadViewModel
        {
            Project = project,
            Directory = new DirectoryInfo(dir),
            Uploader = uploader,
            Time = time,
            NotificationList = notification
        };
    }
    }
}
4

1 に答える 1

1

RenderAction()ビューを部分ビューでレンダリングしてみてください。

また、アクションを[ChildActionOnly]属性で装飾して、アクションが独自に実行されないようにする必要があります(誰かがリクエストした場合http://xxx.com/Upload/Index)。

于 2010-08-09T15:42:49.460 に答える