Conto
Conto

Reputation: 53

Model not being passed to controller

One of my asp.net core pages isn't passing its model back to the controller. I've investigated similar threads in stackoverflow but none could solve my issue - what's different in this particular case?

In the attached MVC, I fill the input box "Enter here some text", then I click on button "CLICK HERE". The debugger enters correctly the controller but the variable "TEST" returns null.

Any geniuses out there able to help me out?

My Model:

namespace MyProgram.Models._04_ModuleTasker
{
    public class TaskerViewModel
    {
        public SelectList SelectListLocations;
        public SelectList SelectListUsers;
        public SelectList SelectListFilters;

        public string SelectedCompanyLocationDesc;
        public string SelectedUserId;
        public string SelectedFilter;

    }
}

My View:

@model MyProgram.Models._04_ModuleTasker.TaskerViewModel
@{
    ViewData["Title"] = "AllTasks";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container-fluid">
    <div class="PageTitle">
        Tasker
        <hr class="TitleHR" />
    </div>
    <div style="text-align:center;margin-top:20px" class="text-body">
        <form asp-controller="Tasker" asp-action="ShowActiveTasksPOST" method="post" role="form">
            <input type="text" asp-for="SelectedUserId" /> Enter here some text <br />
            <input type="submit" class="btn btn-success InputShortWidth LoginButton" value="CLICK HERE" />
        </form>
    </div>
</div>

My Controller:

        [HttpPost]
        public async Task<IActionResult> ShowActiveTasksPOST(TaskerViewModel taskerViewModel)
        {
            string TEST = taskerViewModel.SelectedUserId;         
            return RedirectToAction("Index", "Home");
        }

Upvotes: 0

Views: 224

Answers (1)

Hameed
Hameed

Reputation: 1615

To make the binding works, you need to convert the fields to properties with with setters and getters. E.g.:

public string SelectedCompanyLocationDesc { get; set; }
public string SelectedUserId { get; set; }
public string SelectedFilter { get; set; }

Upvotes: 2

Related Questions