Recover Parameter Filled in Actionresult

Asked

Viewed 124 times

0

I have a problem that I’ve tried many ways to solve and nothing. When loading the page the page is populated in a table a list, but when it is submitted the template is null, but if you consult the property Request.Form.Tostring() contains the values. 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>
  • See https://answall.com/a/102945/5846

  • Thanks, but with a simple change in view the problem has been solved.

1 answer

0


By making a modification in the view, I was able to recover the model 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.