2

EFでASP.NETMVC4を使用していますが、インデックスビューと作成ビューを備えたPostControllerがあります。投稿を追加するために両方を同じページに配置し、同じページに視覚化したいと思います。どうすればいいですか?

アドバイスありがとうございます

___編集__

コントローラー:

        public ActionResult Index()
        {
            return View(db.Posts.ToList());
        }

        [HttpGet]
        public ActionResult Create()
        {
            return PartialView("_Create", **Here I can't declare model**);
        }

        [HttpPost]
        public ActionResult Create(FormCollection values)
        {
            var post = new Post();
            TryUpdateModel(post);

            if (ModelState.IsValid)
            {
                /** somme code **/

                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View("_Create", post);
        }

私の_部分ビューの作成:

@model MyProject.Models.Post

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    /**some stuff **/
}

私のインデックスビュー:

@model IEnumerable<MyProject.Models.Post>

@{
    ViewBag.Title = "Index";
}

<p>
    /** some stuff **/
</p>

@Html.Partial("_Create", **Here I can't declare model**)

そして私のポストモデル:

public int PostId { get; set; }    
public int UserId { get; set; }       
public string Content { get; set; }

「ディクショナリに渡されるモデルアイテムはタイプ'System.Collections.Generic.List`1[MyProject.Models.Post]'ですが、このディクショナリにはタイプ'MyProject.Models.Post'のモデルアイテムが必要です。

4

1 に答える 1

4

それは本当にあなたのコードが今どのように見えるかに依存します。Create次のように、を返す別のアクションメソッドがある場合PartialView

[HttpGet]
public ActionResult Create()
{
    // Do stuff here to populate your model, if necessary
    return PartialView("_Create", model);
}

次に、ビューで、部分ビューを表示するHtml.RenderAction()場所を使用します。_Create

<div id="IndexViewStuff">
    <p>Some stuff in your normal Index view.</p>
    @{Html.RenderAction("Create", "Post");}
</div>

の個別のアクションメソッドがなくCreate、物事をよりクリーンにするための部分的なビューがある場合は、ビューで使用Html.Partial()するだけでIndexうまくいきます。

@Html.Partial("_Create") // If your _Create partial does not require a model
@Html.Partial("_Create", Model.CreateViewModel) // If it does require a model

アップデート

コードを調べた後、それを行うには2つの方法があります(両方を紹介します)。部分ビューに単一のモデルが必要なときに、投稿のリストをIndexビューに渡すために問題が発生します。呼び出し時にモデルを部分ビューに明示的に渡していないため、ビュー(投稿のリスト)でモデルを自動的に使用しようとします。問題を解決する最初の方法では、コードに最小限の変更を加える必要があります。_CreatePostIndex

Createアクションメソッドを次のように変更します。

[HttpGet]
public ActionResult Create()
{
    // You have to pass a new Post
    return PartialView("_Create", new MyProject.Models.Post());
}

次に、あなたのIndex見解では、以下を使用します。

@{Html.RenderAction("Create", "Post");}

2番目の方法は、投稿のリストを公開してビューに表示するビューモデルを使用することです。また、部分ビューで新しい投稿を作成するために使用できるIndex「空の」モデルもあります。私はこの方法が好きですが、それはあなたの呼び出しです。Post_Create

ビューモデル:

public class MyViewModel
{
    public IEnumerable<Post> Posts { get; set; }
    public Post CreatePost { get; set; }
}

あなたのIndex行動方法:

public ActionResult Index()
{
    MyViewModel model = new MyViewModel()
    {
        Posts = db.Posts.ToList(),
        CreatePost = new MyProject.Models.Post()
    };

    return View(model);
}

あなたのIndex見解:

@model The.Namespace.MyViewModel

@{
    ViewBag.Title = "Index";
}

@foreach (var post in Model.Posts)
{
    <p>post.Content</p> // Or however you want to display your posts
}

@Html.Partial("_Create", Model.CreatePost) // Pass the correct model

部分_Create的なビューは同じままです。

于 2012-12-13T03:32:04.537 に答える