0

I'm working on a page that will add the selected item from a dropdownlist to a List<> using button onclick.

The problem is the new selecteditem is overwriting the old value.

I simply would like to display a table from the selected items like this:

#---Model-----Remove-----
1   Model#1       x
2   Model#2       x
3   Model#3       x
4   Model#4       x
5   Model#5       x
-------------------------

Please see my code below,

ModelDescription.cs (model):

 public class ModelDescription
{
    public string modelDesc { get; set; }
}

method in controller:

public ActionResult StockOnHand()
    {
        bindModelDesc();
        return View();
    }

 public void bindModelDesc()
    {
        var mc = db.ModelMaster_tbl.ToList();
        List<SelectListItem> mclist = new List<SelectListItem>();
        mclist.Add(new SelectListItem { Text = "--Select Model Type--", Value = "0" });
        mclist.Add(new SelectListItem { Text = "--Select All Model--", Value = "1" });

        foreach (var m in mc.Select(x => x.modelDesc).Distinct())
        {
            mclist.Add(new SelectListItem { Text = m, Value = m });

            ViewBag.ModelDescs = mclist;
        }
    }

   public ActionResult AddToListSOH(ModelDescription model)
    {
        var result = new List<ModelDescription>();
        var res = db.ModelMaster_tbl.Where(x => x.modelDesc == model.modelDesc).SingleOrDefault();

        result.Add(new ModelDescription { modelDesc = res.modelDesc });

        return PartialView("AddToListSOH", result);
    }

StockOnHand.cshtml (view):

@using (Html.BeginForm("StockOnHand", "PWSupermarket", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="card">
    <div class="card-body">
        <form class="form-horizontal" role="form">
            <h5 class="card-title">Stock On Hand</h5>
            <p class="card-text">Generates the buildable quantity of a unit. </p>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })

            @if (TempData["Message"] != null)
            {
                <span class="text-success"><strong>@TempData["Message"]</strong></span>
            }

            <div class="form-group">
                @Html.LabelFor(model => model.modelDesc, htmlAttributes: new { @class = "col-md-3 control-label" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.modelDesc, ViewBag.ModelDescs as List<SelectListItem>, htmlAttributes: new { @class = "form-control" })
                    <button class="btn btn-outline-primary mt-1" type="button" onclick="AddToList()">Add To List</button>
                </div>

            </div>

            <div id="foo1" class="mt-2">

            </div>

        </form>
    </div>
</div>
}

Javascript to render the list of selected items partial view:

  <script type="text/javascript">

     function AddToList() {
        $.ajax({
            type: "Get",
            url: '@Url.Action("AddToListSOH", "PWSupermarket")',
            data: { modelDesc: $('#modelDesc').val() },
            contentType: "application/html; charset=utf-8",
            success: function (result) {
                $('#foo1').html(result);
            },
            error: function (ex) { alert('failed.'); }
        })
    }

</script>

AddToListSOH.cshtml (Partial View for the list of selected items):

@model IEnumerable<MESProject_P1_csmvc.Models.ModelDescription>

<div>
@{ var count = 0;}
<table class="table table-sm table-striped" @*style="font-size: .7rem;"*@>
    <caption>List of Models</caption>
    <thead class="thead-dark">
        <tr>
            <th>#</th>
            <th>Model</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                   @count
                </td>
                <td>
                    @Html.DisplayFor(i => item.modelDesc)
                </td>
            </tr>

        }
    </tbody>
</table>
</div>

screen shot of the page

LeonardDM
  • 13
  • 5