1

I have a ASP.NET MVC Project .

HomeController.cs

public class HomeController : Controller
{
    private siteDBEntities db = new siteDBEntities();
    // GET: Main/Home
    public ActionResult Index()
    {
        return View();
    }

    public PartialViewResult _theLastPost()
    {
        var a = (from c in db.Posts
                 orderby c.ID_Post descending
                 select new { c.Title,c.Content});
        return PartialView(a.ToList());
    }
}

Index.cshtml

@model IEnumerable<test1.Models.Post>
@{
ViewBag.Title = "Tourism";
Layout = "~/Areas/Main/Views/Shared/_Layout.cshtml";
}
@Html.Partial("_theLastPost")

PartialView _theLastPost.cshtml

    @model IEnumerable<test1.Models.Post>
    @foreach (var item in Model)
    {
    <h2>
        @item.Title
    </h2>
    <p>
        @item.Content
    </p>
    }

Post.cs this is my view model , that i just want get title and content from EF6 framework .

public partial class Post
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Post()
    {
        this.Comments = new HashSet<Comment>();
        this.Keywords = new HashSet<Keyword>();
    }

    public int ID_Post { get; set; }
    public int ID_Category { get; set; }
    public int ID_Writer { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public string Url { get; set; }
    public Nullable<System.TimeSpan> Time { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public string Index_Image { get; set; }

    public virtual Category Category { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Comment> Comments { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Keyword> Keywords { get; set; }
    public virtual Writer Writer { get; set; }
}

I want show the last post , but it show me this error

The model item passed into the dictionary is of type 'test1.Models.Post', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[test1.Models.Post]

Update 1: That error has fixed by using

@{ Html.RenderAction("_theLastPost"); }

Now i have this error : The entity or complex type 'siteDB.Post' cannot be constructed in a LINQ to Entities query.

Fixed: I just edit Controller for partial view(_theLastPost) like below:

public PartialViewResult _theLastPost()
    {
        var a = (from c in db.Posts
                 orderby c.ID_Post descending
                 select c);
        return PartialView(a);
    }
4
  • 1
    Try @model IEnumerable<test1.Models.Post> Commented Dec 11, 2015 at 12:34
  • if you mean in partial view , i added it before Commented Dec 11, 2015 at 12:35
  • TBH: The code provided does not match the error provided. Have you changed something? Looks like your view's model is not an IEnumerable or your @Html.Partial call is not as stated Commented Dec 11, 2015 at 12:48
  • as you see model has passed to partial view is IEnumerable<> @freedomn-m Commented Dec 11, 2015 at 12:57

2 Answers 2

4

In your case you should use RenderAction not Partial becouse Partial just renders View. And as long as you don't pass ViewModel there you see this error.

While RenderAction will call your Controller and returns View.

Your call on Index will be:

@{@Html.RenderAction("_theLastPost");}

Also you return from your Controller anonymouse object. You can't do it becouse all properties vill be propected and you can't get them on View.

It should be like this with ViewModel:

public class ViewModelPost
{
   public string Title {get; set;}
   public string Content {get; set;}
}


public PartialViewResult _theLastPost()
{
    var a = (from c in db.Posts
             orderby c.ID_Post descending
             select new ViewModelPost { c.Title,c.Content});
    return PartialView(a.ToList());
}

And on your View:

@model IEnumerable<ViewModelPost>
Sign up to request clarification or add additional context in comments.

14 Comments

when is use @Html.RenderAction("_theLastPost"); , error : can not convert implicity 'void' to 'object'
Did you mention @{ } around helper? You need it
@AlirezaSoleimaniAsl Are you sure? I mean i belive you now have problem mentioned here
yes . but now i get this error: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
@freedomn-m but this telling the oposit. Any thoughts what's wrong?
|
2

The cause:

When you don't specify a model to @Partial, it uses the view's model:

@Html.Partial("_theLastPost")

is the same as

@Html.Partial("_theLastPost", Model)

when the view/partial @model are not the same, you get this error.


The fix:

As your controller action for the partial is already doing what you want, call that via @Html.Action

Or, pass the data you want in.

Assuming your view's model has data (it doesn't in the provided code):

@Html.Partial("_thisLastPost", Model.OrderByDescending(x=>x.ID_Post).First())

(but make sure the partial is only expecting one item: @model test1.Models.Post)

3 Comments

i set model like this @model test1.Models.Post but i get this error: Object reference not set to an instance of an object.
That's a separate issue. Debug it. Your question here has been answered.
That is so insane. I just spent 15 minutes screaming at visual studio that it was LYING TO ME until I realized that the model that I had passed was null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.