-1

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.

PsychoCoder
  • 10,570
  • 12
  • 44
  • 60

1 Answers1

3

Apply the [DataType(DataType.MultilineText)] attribute to your property

[DataType(DataType.MultilineText)]
public string Description { get; set; }

or use

@Html.TextAreaFor(m => m.ProductDescription, new { @class = "form-control" })
  • For the record I tried adding the DataType attribute to my property in the EF entity and it did nothing, I'll try the @Html.TextAreaFor and see if that suites my needs. Thanks – PsychoCoder Jul 21 '15 at 00:03
  • 1
    Applying the `DataTypeAttribute` to a property of a data model is not really appropriate (its a view specific attribute). You really should be using a view model for this. –  Jul 21 '15 at 00:07
  • I'm new to MVC kind of, how do you use a view model? – PsychoCoder Jul 21 '15 at 00:58
  • 2
    Too long to cover in comments but refer [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jul 21 '15 at 01:02
  • can you check out my edit and tell me how far off I am? – PsychoCoder Jul 21 '15 at 01:47
  • @PsychoCoder You're still missing the [DataTypeAttribute](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.datatypeattribute.aspx), not to mention the ProductDescription field itself. – Tieson T. Jul 21 '15 at 01:51
  • 1
    In you index method change `List model = new List();` but I suggest you just use `List model = products.Select(p => new DisplayProductsViewModel() { Name = ProductName, Image = ....}).ToList();` –  Jul 21 '15 at 01:52
  • @TiesonT. I know, that's for the create view, I've scraped everything and started over with view models. StephenMuecke I modified my code as shown above, it gives no errors but nothing is returned – PsychoCoder Jul 21 '15 at 03:52
  • @PsychoCoder, Are you saying that the value of `model` is `null` or an empty list? –  Jul 21 '15 at 03:59
  • It's an empty list, if I go back to using the EF entity I get what I'm looking for but from what I've been reading using view models is the proper way to work. – PsychoCoder Jul 21 '15 at 04:00
  • Cant see anything wrong with your latest edit, but with the first edit, did you try `List model = new List();`? –  Jul 21 '15 at 04:07
  • @PsychoCoder, We are getting a bit off track with respect to your original question. I have updated my answer with a link to a fiddle showing both options. If your still having issues with populating your view model, it would be best to ask a new question. –  Jul 21 '15 at 05:43