-1

I'm not a seasoned front-end developer, so I'm struggling with this simple issue...

I have a model related to my database which is a list of 'things'. On my View, I add a checkbox for each row in the list. For every item checked, I want to add a row to an associative table. ...Pretty simple/standard logic. I've designed my checkbox names so I know what id's I'm dealing with, so that isn't my problem. What I'm struggling with is how to get visibility to the checkboxes in my controller. Simply put--what/how can I pass back these checkboxes to the controller so I can loop through them there and call my stored procedure to insert the rows?

Adding my code, but I don't think my issue is just about syntax--I'm not understanding how I actually send the list back to a controller method. I don't want to add it to my model, which is the result of a stored procedure call to get the reference information of the things I want to add. To modify that model would be to put in front-end "stuff" (what the user selected), which is irrelevant to my model which is a list of all related items--I only want a list of the id's of the ones that we want to relate in our associative table. I think I'm looking for a javascript, jquery or json that will generate the list for me so then I could send it to the controller when the "Save and Continue" button is clicked--that is the part I don't know how to do.

@model PagedList.IPagedList<LMN.Models.GetRelatedEquipmentByEquipmentItemCategory_Result>
@using PagedList.Mvc;

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "GetRelatedEquipment";
}

<h2>GetRelatedEquipment</h2>

@*@{var EquipmentItemCategoryId = ViewContext.RouteData.Values["EquipmentItemCategoryId"].ToString(); }*@

<table>

    <tr>
        <th>
                Select
            </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().RelatedCategory)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().GroupName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().MedicareCoverageLikely)
        </th>
    </tr>
    @{ var i = 0; }
    @foreach (var item in Model)
    {

        <tr>
            <td class="border">
                @Html.CheckBox("selectedObjects" + item.RelatedCategoryId)
                    @*@Html.CheckBox("selectedObjects" + item.RelatedCategoryId, new { value = "selectedObjects" + item.RelatedCategoryId })*@
                    @*@Html.Hidden("check[" + i.ToString() + "].RelatedCategoryId", item.RelatedCategoryId)*@

            </td>
            <td class="border">
                @Html.DisplayFor(modelItem => item.RelatedCategory)
            </td>
            <td class="border">
                @Html.DisplayFor(modelItem => item.GroupName)
            </td>
            <td class="border">
                @Html.DisplayFor(modelItem => item.MedicareCoverageLikely, true)
            </td>
                @Html.HiddenFor(modelItem => item.RelatedCategoryId)
        </tr>
        i++;
    }
</table>
<table>
    <tr>
        <td>
            @*@using (Html.BeginForm("GetRelatedSeatSupport", "Letter", new { model => model.FirstOrDefault().EquipmentItemCategoryId = 5 }))
            {*@
            @using (Html.BeginForm("TryThis", "Letter"))
            {
                <p>
                    <input type="submit" value="Save and Continue" />
                </p>
            }

            @*@Html.ActionLink("Save and Continue", "GetRelatedSeatSupport", new { EquipmentItemCategoryId = 7 })*@
        </td>
    </tr>
</table>
SherryA
  • 81
  • 1
  • 1
  • 7
  • 1
    do post your code , at which point your stuck,the community would be able to help you. – jero Aug 24 '15 at 21:27
  • 1
    _I've designed my checkbox names so I know what id's I'm dealing with, so that isn't my problem_? Well that almost certainly is your problem. Show your code –  Aug 24 '15 at 23:07

2 Answers2

0

you have to check whether the checkbox is checked or not and attach the value to your model and send back to your controller either by ajax call or which ever way your sending back the modal.

var chk = $('#checkboxid').is(':checked') ? "Y" : "N";
jero
  • 543
  • 3
  • 13
  • 30
0

You are generating checkboxes that have no relationship at all to your model. If you inspect the html you generating you will see (assuming you have 2 objects where the values of RelatedCategoryId are 1 and 2 respectively)

<input type="checkbox" name="selectedObjects1" .... value="True" />
<input type="hidden" name="selectedObjects1" .... value="False" />
<input type="checkbox" name="selectedObjects2" .... value="True" />
<input type="hidden" name="selectedObjects2" .... value="False" />

You model does not have boolean properties named selectedObjects1 and selectedObjects2 so it would never bind to anything.

The @Html.CheckBox() and @Html.CheckBoxFor() methods are for binding to boolean properties not for binding to an int.

Next, your checkboxes are not even inside the form, so would never be submitted to the controller method anyway.

Its unclear exactly what your wanting to do since you have not shown your controller methods or your models, but assuming you displaying a list of equipment and what to post back the selected items, then start with a view model representing what you want to display/edit in the view

public class EquipmentVM
{
  public int ID { get; set; }
  public string Name { get; set; }
  .... // other properties
  public bool IsSelected { get; set; }
}

Then in the controller, initialize a List<EquipmentVM> and map you data models to the view model collection and pass it to the view

@model List<EquipmentVM>
@using Html.BeginForm())
{
  <table>
    .... // table headers
    <tbody>
      @for(int i = 0; i < Model.Count; i++)
      {
        <tr>
          <td>
            @Html.CheckBoxFor(m => m[i].IsSelected)
            @Html.HiddenFor(m => m[i].ID)
          </td>
          <td>@Html.DisplayFor(m => m[i].Name)</td>
          .... // other display properties of your model
        <tr>
      }
     </tbody>
  </table>
  <input type="submit" ... />
}

Then in your POST method

public ActionResult Edit(List<EquipmentVM> model)
{
  IEnumerable<int> selectedIDs = model.Where(e => e.IsSelected).Select(e => e.ID);
  // do something with the selected ID's
}
  • Thank you. I know my checkboxes are not related to my model and that is part of my problem--but my model is generated from entity framework, which reads the database and the checkboxes being selected (or not)are really just telling me to add the relationships to the database--thus, why I say they are really only about the front-end...so I'm having a hard time understanding how I modify my model so that when and if I re-generate it through entity framework, I won't lose my modifications...going to study up more, but I really appreciate your comments! – SherryA Aug 26 '15 at 14:11
  • As far as my checkboxes not being on the form, this has also been part of my comprehension problem--I have a series of pages. the Html.BeginForm is actually so when the submit button is clicked (from this page), navigation will go to the next page (form). I'm having problems understanding how to code the series of page navigations. Any help/links you can provide in that area would be appreciated. – SherryA Aug 26 '15 at 14:24
  • You don't change your data model, your create a view model. I suggest you start with [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). Every view create should have an associated view model especially when its for editing data. And tools such as [automapper](https://github.com/AutoMapper/AutoMapper) make it easy to map between your data models and view models. –  Aug 26 '15 at 21:54
  • As for your multi-step page requirement, there are a number of ways to approach this. Personally I prefer have every thing on one page and having each 'step' displayed one at a time as per [this answer](http://stackoverflow.com/questions/25643394/mvc-force-jquery-validation-on-group-of-elements/25645097#25645097). Another approach is to have a series of view models and save each view model in a temporary repository before moving to the next step. It depends on factors such as how big the form will be, do you want to be able to allow the user to stop and come back to finish it later etc. –  Aug 26 '15 at 22:03
  • I assume your still not understanding this. What are you still struggling with? –  Aug 29 '15 at 07:28