How to use anonymous list as model in an ASP.NET MVC partial view? -
i have list of contact
objects, which, want subset of attributes. used linq projection create anonymous list , passed partial view. when use list in partial view, compiler says doesn't have attributes. tried simplest case follow, still have no chance use anonymous object or list in partial view.
var model = new { firstname = "saeed", lastname = "neamati" }; return partialview(model);
and inside partial view, have:
<h1>your name @model.firstname @model.lastname<h1>
but says @model doesn't have firstname , lastname properties. what's wrong here? when use @model, string render in browser:
{ title = "saeed" }
don't this. don't pass anonymous objects views. properties internal , not visible in other assemblies. views dynamically compiled separate dynamic assemblies asp.net runtime. define view models , type views. this:
public class personviewmodel { public string firstname { get; set; } public string lastname { get; set; } }
and then:
var model = new personviewmodel { firstname = "saeed", lastname = "neamati" }; return partialview(model);
and in view:
@model personviewmodel <h1>your name @model.firstname @model.lastname<h1>
Comments
Post a Comment