0

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.

fnk
  • 43
  • 1
  • 6
  • 1
    Possible duplicate of [How to get DropDownList SelectedValue in Controller in MVC](https://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc) – PTD Jul 25 '17 at 19:00
  • 1
    You form inputs need `name` attributes with values that match the view model being posted, not `id` values. Thats what the whole `@Html.DropDownListFor` and `@Html.EditorFor` do in part. If you are not going to use those Razor Html helpers, you at least need to add the `name` attribute to your `` i.e. ` – zgood Jul 25 '17 at 19:06
  • Not only do you need a `name` attribute, you also need 2 other properties for binding the selected value to. Always use the strongly typed `HtmlHelper` methods to correctly generate your html. –  Jul 25 '17 at 23:02
  • Thank you for the replies. Resigned from selectpicer and I started using the HtmlHelper. Now everything begins to fold together. – fnk Jul 26 '17 at 04:31

0 Answers0