Retrieve parameter filled in in ActionResult

0

I have a problem that I have tried in many ways to solve and nothing. When loading the page the page is filled into a table a list, but when it is submitted the template is null, but if you query the Request.Form.ToString () property the values are listed. link

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>
    
asked by anonymous 04.12.2017 / 18:21

1 answer

0

By making a modification to the view, I was able to retrieve the template via parameter.

@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>
    
04.12.2017 / 21:40