skip to Main Content

I receive variable with null values after model binding. I don’t know why, can someone explain what’s wrong? Thank you.
Title is null, Content is null after form submitting.

Create.cshtml

<form asp-action="Create" method="post">
            @Html.AntiForgeryToken()
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Title" class="control-label"></label>
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Content" class="control-label"></label>
                <textarea asp-for="Content" class="form-control"></textarea>
                <span asp-validation-for="Content" class="text-danger"></span>
            </div>
            <div class="form-group mt-3">
                <input type="submit" value="Create" class="btn btn-primary" />
                <a asp-action="Index" class="btn btn-custom">Back to List</a>
            </div>
        </form>

PostViewModel.cs

namespace ForumApp.ViewModels
{
    public class PostViewModel
    {
        public string Title { get; set; }
        public string Content { get; set; }
    }
}

Post.cs

namespace ForumApp.Models
{
    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public string UserId { get; set; }
        public ApplicationUser User { get; set; }
        public ICollection<Comment> Comments { get; set; } = [];
    }
}

Create action

        // POST: Posts/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(PostViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);
                var post = new Post
                {
                    Title = model.Title,
                    Content = model.Content,
                    UserId = user.Id
                };
                _context.Add(post);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(model);
        }

Even without ViewModel it was not working, adding [Bind("Title,Content")] didn’t help. That’s everything I receive:null model

2

Answers


  1. If it works with <input type="text" name="Title" class="form-control" /> but not with <input asp-for="Title" class="form-control" />, it may be because you didn’t add tag helpers to your project. Check if you have a file called _ViewImports.cshtml in your Views folder with the following content:

    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    
    Login or Signup to reply.
  2. I receive variable with null values after model binding. I don’t know
    why, can someone explain what’s wrong? Thank you. Title is null,
    Content is null after form submitting.

    Well, based on your code snippet and description, I have tried to investigate your issue, and during the simulation, I have just used your view and controller code and it seems I am getting the correct value in Title and Content property.

    I am not sure, if you haven’t shared your ViewModel reference within your view by mistake or not. Without that reference I think, everything is alright.

    I have tired following way and getting the correct value:

    View:

    @model PostViewModel
    
    <form asp-action="Create" method="post">
        @Html.AntiForgeryToken()
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Title" class="control-label"></label>
            <input asp-for="Title" class="form-control" />
            <span asp-validation-for="Title" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Content" class="control-label"></label>
            <textarea asp-for="Content" class="form-control"></textarea>
            <span asp-validation-for="Content" class="text-danger"></span>
        </div>
        <div class="form-group mt-3">
            <input type="submit" value="Create" class="btn btn-primary" />
            <a asp-action="Index" class="btn btn-custom">Back to List</a>
        </div>
    </form>
    

    enter image description here

    Noted: Added the @model PostViewModel reference within the view which your shared view is missing.

    Controller:

    Controller code is almost same, I have removed few unnecessary code in order to test the scenario, you could include them.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(PostViewModel model)
    {
        if (ModelState.IsValid)
        {
            
            return RedirectToAction(nameof(Index));
        }
        return View(model);
    }
    

    Output:

    enter image description here

    enter image description here

    Note: Please refer to this official document for model validation details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search