I am currently using the ListBox in my view and it is returning the Id of the object I selected. it is fine but is there a way to return the whole selected object from the view?

The ListBox list called "Sizes".

View

@model ButiqueShops.ViewModels.ItemsViewModel

@{
    ViewBag.Title = "Create";
}
<div class="container first">

    <h2>Create</h2>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>Items</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.TypeId, "TypeId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("TypeId", null, string.Empty, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.TypeId, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ImagePath, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ImagePath, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ImagePath, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ColorId, "ColorId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("ColorId", null, string.Empty, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.ColorId, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Sizes, "Sizes", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.ListBox("Sizes", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Sizes, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ShopId, "ShopId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("ShopId", null, string.Empty, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.ShopId, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div>
        @if (ViewBag.ReturnUrl == null)
        {
            @Html.ActionLink("Back to List", "Index")
        }
        else
        {
            @Html.ActionLink("Back to List", "Index", "Shops")
        }
    </div>
</div>

Controller

public async Task<ActionResult> Create(ItemsViewModel items, int[] sizes)
{
    var sizesFromDB = db.Sizes.ToList();

    foreach(var size in sizes)
    {
        items.Sizes.Add(sizesFromDB.FirstOrDefault(s => s.Id == size));
    }
    if (IsItemValidated(items))
    {
        var itemsModel = Mapper.Map<Items>(items);
        db.Items.Add(itemsModel);
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    ViewBag.TypeId = new SelectList(db.ItemTypes, "Id", "Name", items.TypeId);
    ViewBag.ShopId = new SelectList(db.Shops, "Id", "Name", items.ShopId);
    ViewBag.ColorId = new SelectList(db.Colors, "Id", "Name", items.ColorId); ;
    ViewBag.Sizes = new MultiSelectList(sizesFromDB, "Id", "ShortName", items.Sizes);
    return View(items);
}

my temporary solution is to manually create the list of objects.

Related posts

Recent Viewed