Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It really depends on how your code looks right now. If you have a separate <code>Create</code> action method returning a <code>PartialView</code>, like so:</p> <pre><code>[HttpGet] public ActionResult Create() { // Do stuff here to populate your model, if necessary return PartialView("_Create", model); } </code></pre> <p>Then in your view you would use <code>Html.RenderAction()</code> where you want the <code>_Create</code> partial view to be displayed:</p> <pre><code>&lt;div id="IndexViewStuff"&gt; &lt;p&gt;Some stuff in your normal Index view.&lt;/p&gt; @{Html.RenderAction("Create", "Post");} &lt;/div&gt; </code></pre> <p>If you don't have a separate action method for <code>Create</code> and just have a partial view to make things cleaner, then simply using <code>Html.Partial()</code> in your <code>Index</code> view will do the trick:</p> <pre><code>@Html.Partial("_Create") // If your _Create partial does not require a model @Html.Partial("_Create", Model.CreateViewModel) // If it does require a model </code></pre> <p><strong>Update</strong></p> <p>After looking through your code, there are two ways you can do it (I'll show you both). Your problem occurs because you're passing a list of posts to your <code>Index</code> view while your <code>_Create</code> partial view requires a single <code>Post</code> model. Since you aren't explicitly passing a model to the partial view when you're calling it, it automatically tries to use the model in the <code>Index</code> view (your list of posts). The first way to solve the problem requires minimal changes to your code.</p> <p>Change your <code>Create</code> action method as follows:</p> <pre><code>[HttpGet] public ActionResult Create() { // You have to pass a new Post return PartialView("_Create", new MyProject.Models.Post()); } </code></pre> <p>Then in your <code>Index</code> view, use:</p> <pre><code>@{Html.RenderAction("Create", "Post");} </code></pre> <p>The second method is to use a view model that exposes the list of posts to display on the <code>Index</code> view, and also has an "empty" <code>Post</code> model that can be used to create a new post in the <code>_Create</code> partial view. I prefer this method, but it's your call.</p> <p>Your view model:</p> <pre><code>public class MyViewModel { public IEnumerable&lt;Post&gt; Posts { get; set; } public Post CreatePost { get; set; } } </code></pre> <p>Your <code>Index</code> action method:</p> <pre><code>public ActionResult Index() { MyViewModel model = new MyViewModel() { Posts = db.Posts.ToList(), CreatePost = new MyProject.Models.Post() }; return View(model); } </code></pre> <p>Your <code>Index</code> view:</p> <pre><code>@model The.Namespace.MyViewModel @{ ViewBag.Title = "Index"; } @foreach (var post in Model.Posts) { &lt;p&gt;post.Content&lt;/p&gt; // Or however you want to display your posts } @Html.Partial("_Create", Model.CreatePost) // Pass the correct model </code></pre> <p>Your <code>_Create</code> partial view will remain the same.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload