Mordagel
Mordagel

Reputation: 301

MVC trying to pass model from razor view to controller

So my story is that I am having trouble with the post to the controller, the view seems to work fine. When the postback happens the tm.BookId is 0 (should be 1) and the list count is 0. First I will display the model:

 public class TransferModel
{
    public TransferModel()
    {
        cbItems = new List<CheckBoxItem>();
    }
    public List<CheckBoxItem> cbItems {get;set;}
    public int BookId;
    public class CheckBoxItem
    {
        public int AttributeId { get; set; }
        public string Attribute { get; set; }
        public bool Selected { get; set; }

    }
}

The Controller part:

public ActionResult AddAttributes(int id = 0)
    {
        db.transMod.BookId = id;
        BookInfo book = db.BookInfoes.Find(id);
        var latts = db.BookAtts.ToList();
        foreach (BookAtt ba in latts)
        {
            db.transMod.cbItems.Add(new TransferModel.CheckBoxItem { Attribute = ba.Attribute, AttributeId = ba.BookAttId, Selected = false });
        }
        List<BookAtt> atInList = book.BookAtts.ToList();
        foreach (TransferModel.CheckBoxItem cb in db.transMod.cbItems)
        {
            if (atInList.Exists(item => item.Attribute == cb.Attribute))
                cb.Selected = true;
        }

        return View(db.transMod);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AddAttributes(TransferModel tm)
    {
       List<BookAtt> atPool = db.BookAtts.ToList();
       BookInfo book = db.BookInfoes.Find(tm.BookId);
       foreach (TransferModel.CheckBoxItem sel in tm.cbItems)
        {
            if (sel.Selected)
            book.BookAtts.Add(atPool.Find(item1 => item1.Attribute == sel.Attribute));
        }
        db.SaveChanges();
        return RedirectToAction("AddAttributes");
    }`enter code here`

And finally the view:

 @model BrightStar.Models.TransferModel
@{
ViewBag.Title = "Update Attributes";
}
<h2>Add Attributes</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<table>
 @Html.HiddenFor(model => Model.BookId)
 @Html.HiddenFor(model => Model.cbItems)

@foreach (var itm in Model.cbItems)
{
    <tr>
        <td>@Html.HiddenFor(mo => itm.AttributeId)</td>
        <td>@Html.CheckBoxFor(mo => itm.Selected)</td>
        <td>@Html.DisplayFor(mo => itm.Attribute)</td>
    </tr>
}

</table>
    <p>
        <input type="submit" value="Save"  />
    </p>
}

enter code here

Upvotes: 0

Views: 358

Answers (2)

Dima
Dima

Reputation: 6741

You should reference your model's properties in helpers to correctly generate names for your controls:

@Html.HiddenFor(model => Model.cbItems)

should be

@Html.HiddenFor(model => model.cbItems)

Upvotes: 0

Sunny
Sunny

Reputation: 4809

Model binding doesn't happen automatically, items needs to be in certain format to get binded to list properties in POST actions. Check this out.

Try checking out the value of BookId property in the DOM to confirm it is 1, otherwise it should bind normally.

Upvotes: 1

Related Questions