In my form I have the ability to add a new product to the system (Using MVC 5) but I have a question. I have this code:
<div class="form-group">
@Html.LabelFor(model => model.ProductDescription, "Description", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductDescription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductDescription, "", new { @class = "text-danger" })
</div>
</div>
Which, as you know creates a textbox, what I want to know is there a way to modify this existing code to add a textarea instead of a textbox?
EDIT
@Stephen Muecke
I created a view model called DisplayProductsViewModel and it looks like this:
using AccessorizeForLess.Data;
namespace AccessorizeForLess.ViewModels
{
public class DisplayProductsViewModel
{
public string Name { get; set; }
public string Price { get; set; }
public ProductImage Image { get; set; }
}
}
Then in my controller I changed it to look like so:
private ApplicationDbContext db = new ApplicationDbContext();
// GET: /Products/
public ActionResult Index()
{
var products = db.Products.Include(p => p.ProductImage).ToList();
List<DisplayProductsViewModel> model = null;
foreach (Product pr in products)
{
model.Add(
new DisplayProductsViewModel()
{
Name = pr.ProductName,
Image = pr.ProductImage,
Price = String.Format("{0:C}", pr.ProductPrice)
});
}
return View(model.ToList());
}
but when I run it model is always null. Can you help me out here, once I can get this going then I will have a better understanding of this all.
EDIT
@Stephen Muecke
I modified my code per your suggestions to this:
private ApplicationDbContext db = new ApplicationDbContext();
// GET: /Products/
public ActionResult Index()
{
var products = db.Products.Include(p => p.ProductImage);
List<DisplayProductsViewModel> model = products.Select(p => new DisplayProductsViewModel()
{
Name = p.ProductName,
Image = p.ProductImage,
Price = string.Format("{0:C}", p.ProductPrice) }).ToList();
return View(model);
}
And now nothing is being returned.