1

I have a edit form where I Have two textboxes and checkbox. I am trying to update book details using checkbox. I am not sure how to pass the textbox values from view to controller using checkbox in Entity Framework. I need to save save those edited textbox box values in DB. Right now my checkboxex are not passing the edit textbox values to controller.

My Edit form looks like this Edit Form

View :

@if (Model.Count > 0)
        {
            <div>
                @foreach (var books in Model)
                {
                        <div class="col-md-2">
                            @Html.EditorFor(model => books.caption)
                            @Html.ValidationMessageFor(model => books.caption)
                        </div>
                        
                        <div class="col-md-2">
                            @Html.EditorFor(model => books.copies)
                            @Html.ValidationMessageFor(model => photos.copies)
                        </div>
                        <div class="col-md-1">
                            <input type="checkbox" class="checkboxes" value="@books.id" name="id" />
                        </div>
               }
          </div>
}

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" name="submitButton" value="UPDATE" />
        <input type="submit" name="submitButton" value="ADD"/>
        <input type="submit" name="submitButton" value="DELETE" />
    </div>
</div>

Contoller :

public ActionResult EditBooks(int id, IEnumerable<int> id, string submitButton, 
                            [Bind(Include ="id,caption,copies")] Books books)

{

     switch (submitButton)

    {  
         case: UPDATE

         if (ModelState.IsValid)

          {

             try

            {

                foreach (var item in id)
                {
                    var update = _db.Books.FirstOrDefault(s => s.id == item);

                    if (update != null)
                    {
                        _db.MyTable.Add(update);
                        _db.SaveChanges();
                    }

                    return RedirectToAction("Edit", new {id});
                }
            }
        }
  }

}

I am not sure my controller code is right.

NVP
  • 13
  • 8
  • 1
    See [this](https://stackoverflow.com/questions/1983999/how-to-use-editorfor-inside-a-foreach) – Steve Greene Jul 12 '18 at 20:24
  • My checkbox is not passing the edited textbox values to controller. – NVP Jul 13 '18 at 20:00
  • In addition to the linked question above, [this](https://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects) states the problem even more clearly. – Jasen Jul 13 '18 at 20:15

0 Answers0