1

I can't get the values in controller from the post method used on the view and I don't know why. I saw a lot of forums and I thought that I was doing the right thing, but it doesn't seem to be the correct way to do because I can't get the values.

First I was using just the DisplayFor but that just shows and not pass the values then I join the HiddenFor to use that like an input like I saw in other forums. But the model always returns zero values.

AtribuiçãoController:

public IActionResult Create(List<Main> main)
{
    return RedirectToAction(nameof(Index));
}

Index.cshtml:

@model ModelsLibrary.Main
<form asp-action="Create">
<table class="table">
    <thead>
        <tr>
            <th>
                <label>Disciplina</label>
            </th>
            <th>
                <label>Turno</label>
            </th>
            <th>
                <label>Tipologia</label>
            </th>
            <th>
                <label>Docente</label>
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Turno)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.MetaDisciplina.Nome)
                    @Html.HiddenFor(modelItem => item.MetaDisciplina.Nome)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LetraTurno)
                    @Html.HiddenFor(modelItem => item.LetraTurno)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Tipologia.Tipo)
                    @Html.HiddenFor(modelItem => item.Tipologia.Tipo)
                </td>
                <td>
                    <select asp-for="Docente" asp-items="ViewBag.Docente"></select>
                </td>
            </tr>
        }
    </tbody>
</table>
<div class="form-group">
    <input type="submit" value="Create" class="btn btn-default" />
</div>
</form>

Index Method

 [HttpGet]
    public IActionResult Index()
    {
        var Turno = new List<Turno>();

        Main main = new Main();
        main.Turno = Turno;

        ViewData["Ano"] = new SelectList(_context.AnoLetivo, "Ano", "Ano");
        return View(main);
    }

Hi have a problem binding the values to the controller. The method create should get values in the attributes but i don't know why and heaven don't know why the values not pass... I not pass a list to the view but i want one list in the create method.... If someone can help.

  • Why would you degrade performance by including a whole lot of hidden inputs, sent it to the client and sens it all back again unchanged (just get the collection again in the POST method is you need it? –  Aug 04 '18 at 09:32

1 Answers1

2

Your problem is in your Controller. Your create method do redirect to your Index method but you don't pass your List main

I think your code should be like this:

    [HttpGet]
    public ActionResult Index()
    {
        List<Main> main = (List<Main>)TempData["yourData"];
        if (main == null)
        {
            main = new List<Main>();
        }
        return View("Index", main);
    }
    public IActionResult Create(List<Main> main)
    {
        TempData["yourData"] = main;
        return RedirectToAction(nameof(Index));
    }

The way your want to redirect your data is called the POST-REDIRECT-GET pattern. If your want more information about it, visit this website : https://andrewlock.net/post-redirect-get-using-tempdata-in-asp-net-core/

--------------------EDIT--------------------

You have a binding problem while passing data from View to Controller. You can use an TempData in your View to pass your complete Model to your controller:

@model  List<WebApplication1.Models.TestObject>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@foreach (var item in Model)
{
    @Html.DisplayFor(m => item.ToDisplay)
}


<form action="/Home/Create" method="post">
    <button type="submit">Submit</button>
</form>

<script>
    $('form').submit(function() {
    @{ TempData["FullModel"] = Model; }
    });
</script>

In this exemple, i use a jquery script to create a TempData and send it to the controller

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        List<TestObject> main = (List<TestObject>)TempData["yourData"];
        if (main == null)
        {
            main = new List<TestObject>();
            main.Add(new TestObject("1"));
            main.Add(new TestObject("2"));
            main.Add(new TestObject("3"));
            main.Add(new TestObject("4"));
            main.Add(new TestObject("5"));
        }
        return View("Index", main);
    }

    [HttpPost]
    public ActionResult Create()
    {
        List<TestObject> main = (List<TestObject>)TempData["FullModel"];
        TempData["yourData"] = main;
        return RedirectToAction(nameof(Index));
    }
}

Hope it was helpfull

AntoOne
  • 99
  • 10
  • On your "Create" POST, is List
    main null? if your list is null, that means your have a binding probleme while passing data from View (Index) to Controller (Create)
    – AntoOne Aug 03 '18 at 21:51
  • Yeah... The list
    main os null i only can get values with of docente select if use another attribute... But i don't know why came null the Main....
    – João Mendes Aug 03 '18 at 22:26
  • 2 up votes for a wrong answer that has nothing to do with the question? –  Aug 04 '18 at 09:33
  • How can I just pass the model like an attribute to create method?? Because that is the problem... Nothing pass – João Mendes Aug 04 '18 at 14:24