0

I have a model defined:

public class ExhibitionItemModel
{
    public IEnumerable<tblItem> Items { get; set; }
    public tblExhibition Exhibition { get; set; }
    public IEnumerable<tblExhibitionItem> ExhibitionItems { get; set; }
}

tblItem contains information about a specific item, e.g., description, code, etc. tblExhibitionItem contains an id from tblItem, an exhibition id tells the system for which exhibitionm a specific item belongs, and a price for that item/exhibition combination.

In my Controller, I am populating ExhibitionItemModel:

ExhibitionItemModel exhibitionItemModel = new ExhibitionItemModel();
exhibitionItemModel.Exhibition = db.tblExhibitions.Find(id);
exhibitionItemModel.ExhibitionItems = (from objExhibitionItems in db.tblExhibitionItems
                                               where objExhibitionItems.ExhibitionID == id
                                               select objExhibitionItems).AsEnumerable<tblExhibitionItem>();

exhibitionItemModel.Items = (from objItem in db.tblItems
                                     select objItem).OrderBy(item => item.Code).AsEnumerable<tblItem>();

return View(exhibitionItemModel);

Im my view, I list out all the items and I want to have a checkbox which is selected according to whether the item is in tblExhibitionItem. Then a textbox to enter the price for the item.

So far, I have:

@foreach (var item in Model.Items) 
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.Code)</td>
            <td>@Html.DisplayFor(modelItem => item.Description)</td>
            <td>@Html.CheckBoxFor(modelItem => item.AdditionalItem.Value, new { @disabled = "disabled" })</td>
            <td>@Html.CheckBox("chkIsSelected")</td>
            <td>@Html.Editor("txtPrice")</td>
        </tr>
    }

As I am a newbie to MVC, I am stuck as to how I establish the link between the checkbox and the Price textbox for an ExhibitionItem to that of the Item. In other words, when I commit the changes to a database, I am populating the ExhibitionItems with the ID of the Item and the Price, depending on whether the Checkbox ("chkIsSelected") is selected.

Additionally, what do I need to change to get the checkbox and Price populated if there are already exhibitionitems in the database?

1 Answer 1

0

You can achieve this using ajax.

You need to figure out what checkbox is for what value. So the best way to link it is by using the ID.

So your checkbox code may look like this:

<td>@Html.CheckBox("chkIsSelected", new { @value = item.ID })</td>

Now you'd use jquery to get an array of selected checkbox ids, and pass that to your controller.

$('#submitButton').click(function(e) {
    e.preventDefault();
    var selectedItems = new Array(); // Create Array

    // Add to array
    $('#chkIsSelected:checked').each(function() { selectedItems.push($(this).val()); });

    $.ajax({
          url: '/update-exhibition-item',
          data: { items: selectedItems },
          traditional: true,
          success:function (data) {
                // do something.
          });
    });

});

Now you have to read this into your controller. This could be a partialview, or a proper view with results. It's up to you. Regardless, you'd cycle through the array and update each value that exists in the array. e.g.

[ActionName("update-exhibition-item")]
public ActionResult UpdateExhibition(int[] items)
{
     var list = from a in tableA
                where items.Contains(a.id)
                select a;
     // etc.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.