मेरे पास एक व्यू मॉडल है जिसमें उत्पाद श्रेणी का प्रकार और एक आईनेमेरेबल < उत्पाद> प्रकार शामिल है। पोस्ट पर पहली स्तर की उत्पाद वस्तु वापस व्यूमोडल से बंधी हुई है जबकि उत्पाद enum वापस शून्य आ रहा है।मॉडल देखें Inumerable <> संपत्ति पोस्ट विधि से वापस शून्य (बाध्यकारी नहीं) आ रही है?
आईन्यूमेरेबल < क्यों गर्व है> संपत्ति प्रति पोस्ट मेरे दृश्य मॉडल से बंधी नहीं है? धन्यवाद!
मॉडल:
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;}
}
दृश्य:
@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>
}
}
नियंत्रक:
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();
}
आप अपने EditorFor अनुकूलित किया की तरह इस संपादक टेम्पलेट कॉल कर सकते हैं? – Shyju
@Shyju: नमस्ते मैंने नहीं किया..मैं यह मान रहा हूं कि अगर मैं Product.cshtml के लिए एक संपादक टेम्पलेट बनाउंगा तो मैं @ HTml.EditorFor (मॉडल => मॉडल.प्रोडक्ट्स) को कॉल कर सकता हूं और यह शायद काम करेगा? – JaJ
हाँ आप कर सकते हैं। मैंने उस दृष्टिकोण के साथ एक जवाब जोड़ा। – Shyju