Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:36:10+00:00 2026-06-16T04:36:10+00:00

I’am using ASP.NET MVC 4 with EF, I have a PostController with the Index

  • 0

I’am using ASP.NET MVC 4 with EF, I have a PostController with the Index and Create views. I would like to have both on the same page for adding a post, and visualize it on the same page. How can I do it ?

Thanks for your advices

___EDIT__

Controller :

        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);
        }

My _Create partial view :

@model MyProject.Models.Post

<h2>Create</h2>

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

    /**some stuff **/
}

my Index View :

@model IEnumerable<MyProject.Models.Post>

@{
    ViewBag.Title = "Index";
}

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

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

And my Post Model :

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

It tells me that “The model item passed into the dictionary is of type ‘System.Collections.Generic.List`1[MyProject.Models.Post]’ but this dictionary requires a model item of type ‘MyProject.Models.Post‘.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:36:11+00:00Added an answer on June 16, 2026 at 4:36 am

    It really depends on how your code looks right now. If you have a separate Create action method returning a PartialView, like so:

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

    Then in your view you would use Html.RenderAction() where you want the _Create partial view to be displayed:

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

    If you don’t have a separate action method for Create and just have a partial view to make things cleaner, then simply using Html.Partial() in your Index view will do the trick:

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

    Update

    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 Index view while your _Create partial view requires a single Post 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 Index view (your list of posts). The first way to solve the problem requires minimal changes to your code.

    Change your Create action method as follows:

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

    Then in your Index view, use:

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

    The second method is to use a view model that exposes the list of posts to display on the Index view, and also has an “empty” Post model that can be used to create a new post in the _Create partial view. I prefer this method, but it’s your call.

    Your view model:

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

    Your Index action method:

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

    Your Index view:

    @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
    

    Your _Create partial view will remain the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to run a str_replace or preg_replace which looks for certain words
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I would like to count the length of a string with PHP. The string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.