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; }
}
}