Home > front end >  Model does not get any properties fillen after post
Model does not get any properties fillen after post

Time:11-11

Problem: Hi fellow programmers, I am new to ASP.NET and tried to figure out why this happens for hours but could not find why. The model in the controller does not get any property after the post. They are all nulls, why is that so?

The view

@model AddProductViewModel

@{
    ViewBag.Title = "Add a new Product";
}
<h2 >@ViewBag.Title</h2>
<div >
    <div >
        <form asp-action="Add" method="post">
            <div >
                <label asp-for="@Model.Model" >Model Name</label>
                <input asp-for="@Model.Model"  aria-required="true" />
                <span asp-validation-for="Model" ></span>
            </div>
            <div >
                <label asp-for="@Model.Description" >Description</label>
                <textarea asp-for="@Model.Description"  aria-required="true" rows="5"></textarea>
                <span asp-validation-for="Description" ></span>
            </div>
            <div >
                <label asp-for="@Model.Colour" >Colour</label>
                <input asp-for="@Model.Colour"  value="" />
                <span asp-validation-for="Colour" ></span>
            </div>
            <div >
                <label asp-for="@Model.Size" >Size</label>
                <input asp-for="@Model.Size"  value="" />
                <span asp-validation-for="Size" ></span>
            </div>
            <div >
                <label asp-for="@Model.Price" >Price</label>
                <input asp-for="@Model.Price"  value="" />
                <span asp-validation-for="Price" ></span>
            </div>
            <div >
                <label asp-for="@Model.ImageData" >Image URL</label>
                <input asp-for="@Model.ImageData"  aria-required="true" />
                <span asp-validation-for="ImageData" ></span>
            </div>
            <div >
                <label asp-for="@Model.CategoryId" >Category</label>
                <select asp-for="@Model.CategoryId" >
                    @foreach (var category in Model.Categories)
                    {
                        <option value="@category.Id">@category.Name</option>
                    }
                </select>
                <span asp-validation-for="CategoryId" ></span>
            </div>
            <div >
                <label asp-for="@Model.BrandId" >Brand</label>
                <select asp-for="@Model.BrandId" >
                    @foreach (var brand in Model.Brands)
                    {
                        <option value="@brand.Id">@brand.Name</option>
                    }
                </select>
                <span asp-validation-for="BrandId" ></span>
            </div>
            <div >
                <input  type="submit" value="Add" />
            </div>
        </form>
    </div>
</div>
@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

The controller

...
        [HttpPost]
        public async Task<IActionResult> Add(AddProductViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            try
            {
                await productService.AddProductAsync(model);

                return RedirectToAction("Index", "Home");
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Something went wrong");

                return View(model);
            }
        }
...

That's the first time something like that happens to me. I have done another project which was very similar if not the same. Any help will be appreciated!

CodePudding user response:

Your model contains property named Model which will conflict with your parameter AddProductViewModel model.

So just change your parameter name like below:

[HttpPost]
public async Task<IActionResult> Add(AddProductViewModel addProductViewModel)
{
    if (!ModelState.IsValid)
    {
        return View(addProductViewModel);
    }

    try
    {
        await productService.AddProductAsync(addProductViewModel);
        return RedirectToAction("Index", "Home");
    }
    catch (Exception)
    {
        ModelState.AddModelError("", "Something went wrong");

        return View(addProductViewModel);
    }
}
  • Related