2012-03-28 12 views
11

Ü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(); 
    } 
+0

gibi bu Editör şablonu arayabilirim? – Shyju

+0

@Shyju: Merhaba Ben didnt..Ben bir EditorTemplate for Product.cshtml oluşturursam @ HTml.EditorFor (model => model.Products) çağırabilir ve muhtemelen çalışır? – JaJ

+0

Evet yapabilirsin. Bu yaklaşıma bir cevap ekledim. – Shyju

cevap

14

. Ürünler listesinden modele erişmeniz gerekiyor. Deneyin bunu böyle yapıyor:

@count = 0 
foreach (var item in Model.Products) 
    { 
    <div> 
    @Html.LabelFor(model => model.Products[count].ID) 
    @Html.EditorFor(model => model.Products[count].ID) 
</div> 
<div> 
    @Html.LabelFor(model => model.Products[count].Name) 
    @Html.EditorFor(model => model.Products[count].Name) 
</div> 
<div> 
    @Html.LabelFor(model => model.Products[count].Price) 
    @Html.EditorFor(model => model.Products[count].Price) 
</div> 
@count++ 
    } 

Düzenleme

Denetleyici:

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) 
    { 
     // work with view model 

     return View(); 
    } 

Modeli

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 List<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:

Bu sorunu gidermek üzere
+1

Neden düzenli bir döngü taklit etmek için bir foreach döngü kullanın? Neden öğeyi foreach döngüsünde bulduğunuzda neden tekrar model.Products [sayısı] diye çağırırsınız? – rossisdead

+0

@Xander - Teşekkürler. Yardımcılar, '.' Genişlemesini kullanmaya gerçekten iyi cevap vermezler, dolayısıyla 'model.Products.item.ID', indeksleme dolayısıyla işe yaramaz. Ancak .item'i çözümden kaldırmayı unuttum ve ilk yorumunuzda yorum yaptığınız değişikliği yansıtacak şekilde düzenledim. –

+1

@rossisdead - Yukarıda yorumlandığı gibi, 'model.Products.item' burada kullanılacak kadar güzel değil. A 'için (int say = 0; count

7

Travis's answer. Burada başka bir yaklaşım olan EditorTemplates

Ürünler'i görüntülemek için Editor template kullanabilirsiniz ve iyi çalışacaktır.

1)

ekleyin Görüntüleme/Ev klasörüne

2) altında içerde "Products" adlı bir görünüm "EditorTemplates" adlı bir Klasör Oluştur. bu görünümde

@model SO_MVC.Models.Product 
@{ 
    Layout = null; 
} 
<p> 
    @Html.LabelFor(x=>x.ID) 
    @Html.EditorFor(x=>x.ID) 
</p> 
<p> 
    @Html.LabelFor(x=>x.Name) 
    @Html.EditorFor(x=>x.Name) 
</p> 
<p> 
    @Html.LabelFor(x=>x.Price) 
    @Html.EditorFor(x=>x.Price) 
</p> 
@Html.HiddenFor(x=>x.Id) 

ve Ana Görünümünüzdeki Bu kodu, size EditorFor özelleştirmek mı bu

@Html.EditorFor(m=>m.Products) 
+0

Evet, bu da işe yarıyor! ! – JaJ

İlgili konular