Ürün sınıfı türü ve IEnumerable < Ürün> türü içeren bir görünüm modelim var. Gönderide, birinci seviye ürün nesnesi, görünüm enlemine geri döndüğünde, görünüm envanteri bağlanır.Modele Bak IEnumerable <> özellik geri gönderim yönteminden null (değil) geri geliyor?
IEnumerable < Prouduct> özelliği neden posta başına benim görünüm modelime bağlanmıyor? Teşekkürler!
Modeller:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class ProductIndexViewModel
{
public Product NewProduct { get; set; }
public IEnumerable<Product> Products { get; set; }
}
public class BoringStoreContext
{
public BoringStoreContext()
{
Products = new List<Product>();
Products.Add(new Product() { ID = 1, Name = "Sure", Price = (decimal)(1.10) });
Products.Add(new Product() { ID = 2, Name = "Sure2", Price = (decimal)(2.10) });
}
public List<Product> Products {get; set;}
}
Görünüm:
@model ProductIndexViewModel
@using (@Html.BeginForm())
{
<div>
@Html.LabelFor(model => model.NewProduct.Name)
@Html.EditorFor(model => model.NewProduct.Name)
</div>
<div>
@Html.LabelFor(model => model.NewProduct.Price)
@Html.EditorFor(model => model.NewProduct.Price)
</div>
<div>
<input type="submit" value="Add Product" />
</div>
foreach (var item in Model.Products)
{
<div>
@Html.LabelFor(model => item.ID)
@Html.EditorFor(model => item.ID)
</div>
<div>
@Html.LabelFor(model => item.Name)
@Html.EditorFor(model => item.Name)
</div>
<div>
@Html.LabelFor(model => item.Price)
@Html.EditorFor(model => item.Price)
</div>
}
}
Denetleyici: Sen düzgün lambda ifade kullanmıyorsunuz
public class HomeController : Controller
{
BoringStoreContext db = new BoringStoreContext();
public ActionResult Index()
{
ProductIndexViewModel viewModel = new ProductIndexViewModel
{
NewProduct = new Product(),
Products = db.Products
};
return View(viewModel);
}
[HttpPost]
public ActionResult Index(ProductIndexViewModel viewModel)
{
// ???? viewModel.Products is NULL here
// ???? viewModel.NewProduct comes back fine
return View();
}
gibi bu Editör şablonu arayabilirim? – Shyju
@Shyju: Merhaba Ben didnt..Ben bir EditorTemplate for Product.cshtml oluşturursam @ HTml.EditorFor (model => model.Products) çağırabilir ve muhtemelen çalışır? – JaJ
Evet yapabilirsin. Bu yaklaşıma bir cevap ekledim. – Shyju