خواندن اطلاعات از سرور و نمایش آن توسط Angular در ASP.NET MVC
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۲/۱۱/۱۸ ۱۶:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<div ng-init="products=[
{id:1,name:'product1',price:25000,description:'description of product'},
{id:2,name:'product2',price:5000,description:'description of product'},
{id:3,name:'product3',price:5000,description:'description of product'},
{id:4,name:'product4',price:2000,description:'description of product'},
{id:5,name:'product5',price:255000,description:'description of product'}
]">
<div>
<div>
<table>
<tr>
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Description</th>
</tr>
<tr ng-repeat="product in products">
<td>{{product.id}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product.description}}</td>
</tr>
</table>
</div>
</div>
</div> @foreach (var product in Model.products)
{
<td>product.id</td>
<td>product.name</td>
<td>product.price</td>
<td>product.description</td>
} namespace AngularAndMvc.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public string Description { get; set; }
}
} namespace AngularAndMvc.Controllers
{
public class ProductController : Controller
{
public ActionResult Index()
{
return View("Index", "", GetSerializedProduct());
}
public string GetSerializedProduct()
{
var products = new[]
{
new Product{Id=1,Name="product1",Price=4500,Description="description of this product"},
new Product{Id=2,Name="product2",Price=500,Description="description of this product"},
new Product{Id=3,Name="product3",Price=400,Description="description of this product"},
new Product{Id=4,Name="product4",Price=5500,Description="description of this product"},
new Product{Id=5,Name="product5",Price=66500,Description="description of this product"}
};
var settings = new JsonSerializerSettings { ContractResolver=new CamelCasePropertyNamesContractResolver()};
return JsonConvert.SerializeObject(products,Formatting.None,settings);
}
}
} var settings = new JsonSerializerSettings { ContractResolver=new CamelCasePropertyNamesContractResolver()};
return JsonConvert.SerializeObject(products,Formatting.None,settings); @model string
<div ng-init="products = @Model">
<div>
<div>
<table>
<tr>
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Description</th>
</tr>
<tr ng-repeat="product in products">
<td>{{product.id}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product.description}}</td>
</tr>
</table>
</div>
</div>
</div>
سورس مثال فوق را هم از اینجا می توانید دریافت کنید.
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult Index()
{
return View("Index", "", GetSerializedProduct());
} <select id="cities">
<option value="246c899b-f6e8-e511-b696-001f29ea72a6">Tehran</option>
<option value="256c899b-f6e8-e511-b696-001f29ea72a6">Kurdistan</option>
</select> $scope.cities = [
"Tehran",
"Sanandaj"
];
<select>
<option ng-repeat="city in cities">{{city}}</option>
</select>