0

I am new to MVC/Razor/Web and am hoping to eventually be able to edit a list of items, right now I am just trying to display these items using Html.BeginCollectionItem and it is not working (no error, just does not display my items.) I have listed my code below:

Model:

namespace EditList
{
    public class GiftModel
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }
}

Controller:

namespace EditList
{
    public class GiftController : Controller
    {
        public ActionResult Index()
        {
            GiftModel[] initalData = new[] 
            {
                new GiftModel{Name = "Tall Hat", Price = 39.95},
                new GiftModel{Name = "Long Cloak", Price = 120.00}
            };

            return View(initalData);
        }
    }
}

Main View:

@{
    Layout = null;
}
@model IEnumerable<GiftModel>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <h2>Gift List</h2>
    What do you want for your birthday?

    @using(Html.BeginForm())
    {
        <div id="EditorRows">
            @foreach (var item in Model)
            {
                Html.RenderPartial("GiftEditorRow", item);
            }
        </div>

        <input type="submit" value="Finished"/>
    }

</body>
</html>

Partial View:

@model GiftModel
<div class="editorRow">
    @using(Html.BeginCollectionItem("gifts"))
    {
        <div>HELLO</div>
        Html.DisplayFor(m => m.Name);
        Html.TextBoxFor(m => m.Name);
        //Html.TextBoxFor(m => m.Price, new { size = 4 });
    }
</div>

One thing to note is i did step into my main view and there is data in "items", but the partial view does not display the textboxes ect.. Im new to web code so i'm hoping I am missing something simple (I have looked at many tutorials and maybe i'm just overlooking something)

Let me know if there is any additional info I can provide.

hrh
  • 658
  • 1
  • 14
  • 24
  • Does the model get transferred all the way to the partial view? When you step into the Partial View, do you have the object as the Model? – Alex Jul 05 '16 at 16:53
  • when I step into the partial view and mouse over m or m.Name nothing pops up. but in the main view if i mouse over item I see my data – hrh Jul 05 '16 at 16:59
  • I took a better look if im in my partialView and I go to "Autos" this.Model.Name I see data. – hrh Jul 05 '16 at 17:01
  • Okay refer to [this similar question](http://stackoverflow.com/questions/29774822/mvc-5-begincollectionitem-with-partial-crud) and see if solves your issue. – Alex Jul 05 '16 at 17:10

0 Answers0