I put the first steps in ASP. NET and MVC and i can not jump over one issue.
How can I transfer data from two class = "selectpicker" to the controller? Here's my code and my attempts to:
Model:
public class AllocatingCodesInMachinesModel
{
public List<string> machineList { get; set; }
public List<string> codesCounterList { get; set; }
public int numbersOfAvaibleCodes { get; set; }
}
Controller:
[HttpGet]
public ActionResult AllocatingCodesInMachines()
{
int numbersOfAvaibleCodes = //get data from DB;
var codesCounter = //get data from DB.ToList();
var machineList = //get data from DB.ToList();
model.machineList = machineList;
model.codesCounterList = codesCounter;
model.numbersOfAvaibleCodes = numbersOfAvaibleCodes;
return View(model);
}
[HttpPost]
public ActionResult AllocatingCodesInMachines(AllocatingCodesInMachinesModel model)
{
var option1 = model.codesCounterList;
var option2 = model.machineList;
return View();
}
public ActionResult TestAction(string x, string y)
{
var option1 = x;
var option2 = y;
return View("AllocatingCodesInMachines");
}
View:
@using Swifferr.Models.ViewModels
@model AllocatingCodesInMachinesModel
@{
ViewBag.Title = "AllocatingCodesInMachines";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Przypisywanie kodów do maszyn</h2>
@using (Html.BeginForm("AllocatingCodesInMachines", "ActualCodes", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="row">
<div class="col-sm-12">
<p><h4>Dostępne kody</h4></p>
<h5 class="text-center">Liczba kodów dostępnych do przydzielenia: <b>@Model.numbersOfAvaibleCodes</b></h5>
<p><h4>Przydzielanie kodów</h4></p>
<div class="col-sm-4">
<p>Liczba kodów przydzielanych do maszyny </p>
<select id="numbersOfCodes" class="selectpicker">
<option>Wybierz...</option>
@foreach (var item in @Model.codesCounterList)
{
<option value="@item.ToString()">@item.ToString()</option>
}
<option value="Wszystkie">Wszystkie</option>
</select>
</div>
<div class="col-sm-4">
<p>Maszyna: </p>
<select id="machine" class="selectpicker">
<option>Wybierz...</option>
@foreach (var item in @Model.machineList)
{
<option value="@item.ToString()">@item.ToString()</option>
}
</select>
</div>
<br /><br />
//***1***
<button type="submit" class="btn btn-info">Dodaj</button>
//***2***
<input type="button" class="btn btn-danger" value="Usuń" onclick="location.href='@Url.Action("TestAction", "ActualCodes", new {x = "numbersOfCode", y = "machine"})'" />
</div>
</div>
}
I tried, but unfortunately it did not work out:
1 Pass the model from the view to the controller, but do not know how to "make up" model on the side view.
2 I tried to pass the selected items from the "selectpicker"'s but do not know how to transfer the data using their id
I know it's base. For several days I try to solve this problem and unfortunately I fell.
Thank you in advance for any advice tips and fixes in my thinking and identifying errors in my code.
PS. Sorry for my English.