0

Estou com um problema que já tentei de várias formas resolver e nada. Ao carregar a pagina a pagina é preenchida numa table uma lista, mas quando é submetida o modelo está nulo, mas se consultar a propriedade Request.Form.ToString() consta os valores. https://dotnetfiddle.net/wGJbYK

namespace RequesIEnumerable
{
    public class CategoryViewModel
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
    }
}

.

using System.Collections.Generic;
using System.Web.Mvc;

namespace RequesIEnumerable
{
    public class CategoryController : Controller
    {
        // GET: Category
        public ActionResult Index()
        {
            List<CategoryViewModel> categories = new List<CategoryViewModel>();
            categories.Add(new CategoryViewModel { CategoryId = 1, CategoryName = "CategoryOne" });
            categories.Add(new CategoryViewModel { CategoryId = 2, CategoryName = "CategoryTwo" });
            categories.Add(new CategoryViewModel { CategoryId = 3, CategoryName = "CategoryThree" });
            categories.Add(new CategoryViewModel { CategoryId = 4, CategoryName = "CategoryFour" });

            IEnumerable<CategoryViewModel> ieCategories = categories;

            return View(ieCategories);
        }
        [HttpPost]
        public ActionResult Index(IEnumerable<CategoryViewModel> model)
        {
            var count =  Request.Form.ToString().Split('&').Length;

            if (model == null)
                System.Console.WriteLine("model is null");

            return RedirectToAction("Index");
        }

    }
}

.

@model IEnumerable<RequesIEnumerable.CategoryViewModel>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.CategoryId)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CategoryName)
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.EditorFor(model => item.CategoryId)
                    </td>
                    <td>
                        @Html.EditorFor(model => item.CategoryName)
                </td>
            </tr>
            }

        </table>
        <input type="submit" name="submit" value="Submit" />
    }
</body>
</html>
2

1 Resposta 1

0

Realizando uma modificação na view, consegui recuperar o modelo via parâmetro.

@model IEnumerable<CategoryViewModel>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.CategoryId)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CategoryName)
                </th>
            </tr>

            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>
                        @Html.EditorFor(it => it.ToList()[i].CategoryId)
                    </td>
                    <td>
                        @Html.EditorFor(it => it.ToList()[i].CategoryName)
                    </td>
                </tr>
            }
        </table>
        <input type="submit" name="submit" value="Submit" />
    }
</body>
</html>

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .