1

I have a Controller with an ActionResult which gets three arrays from the view at HttpPost. The arrays are the Id(tableId) the position from the top(positionY) and the position from the left(positionX). When you click save the ActionResult needs to update all the Positions from the corresponding id's in the database.

This is my Controller:

private BonTempsDbContext db = new BonTempsDbContext();

[HttpPost]
public ActionResult Index(int[] tableId, int[] tablePosX, int[] tablePosY)
{
    int i = 0;
    foreach (var id in tableId)
    {
        int number = tableId[i];
        Tafel tafel = db.Tafel
            .Where(x => x.id == number)
            .ToList();
        db.Entry(tafel).State = EntityState.Modified;
        i++;
    }
    return View(db.Tafel.ToList());
}

I get the error:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'BonTempsMVC.Tafel'

This is my View if you van get something usefull out of it.

@model IEnumerable<BonTempsMVC.Tafel>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>Index</h2>
<div class="VoegToeBtn">
    <a href="/tafel/create">
        <span class="btn btn-default">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Maak nieuw menu aan
        </span>
    </a>
</div>
@using (Html.BeginForm())
    {
    @Html.AntiForgeryToken()

    <div id="tablewrapper">

                @foreach (var item in Model)
                {
                    var positionXId = "posX" + item.id;
                    var positionYId = "posY" + item.id;
                    <div class="draggable ui-widget-content" id="@Html.DisplayFor(ModelItem => item.id)">
                        <input type="text" hidden name="tableId" value="@Html.DisplayFor(ModelItem => item.id)" />
                        <input type="text" hidden name="tablePosX" id="@positionXId" value="" />
                        <input type="text" hidden name="tablePosY" id="@positionXId" value="" />
                        <p>@Html.DisplayFor(ModelItem => item.tafelNaam)</p>
                    </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>
}
<script>
$(".draggable").draggable({
    snap: ".draggable",
    snapMode: "outer",
    stop: function (event, ui) {
        var finalOffset = $(this).offset();
        var finalxPos = finalOffset.left;
        var finalyPos = finalOffset.top;
        var itemId = $(this).attr('id');
        var posXid = "posX" + itemId;
        var posYid = "posY" + itemId;
        $('input:text[id="' + posXid + '"]').attr("value", finalxPos);
        $('input:text[id="' + posYid + '"]').attr("value", finalyPos);
    },
});
</script>

And tafel model:

namespace BonTempsMVC
{
    public class Tafel
    {
        public int id { get; set; }

        public string tafelNaam { get; set; }

        public bool beschikbaar { get; set; }

        public float positionY { get; set; }

        public float positionX { get; set; }
    }
}
2
  • 1
    Can you post your definition of Tafel object? Commented Dec 1, 2014 at 17:38
  • I edited my question with my tafel model. Commented Dec 1, 2014 at 17:40

1 Answer 1

3

Try this:-

 List<Tafel> tafel = db.Tafel
                .Where(x => x.id == number)
                .ToList();

Here, List<Tafel> is returned but you are storing that in Tafel object.
If you are pretty sure that in Tafel entity there will be only one item with matching id then do this:-

Tafel tafel = db.Tafel.FirstOrDefault(x => x.id == number);

As, suggested by @DavidG you can also do this:-

Tafel tafel = db.Tafel.Find(number);
Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively, Tafel tafel = db.Tafel.Find(number); will work too.
Thanks! Really helpful and quick! Now need to figure out how to make the query to update the positions from each table.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.