0

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");
        }
5
  • What does the generated html for the form look like? Jun 2, 2019 at 11:26
  • You are not passing model from your controller to view. Jun 2, 2019 at 12:20
  • Aleksa - What should I change to pass the model to view?
    – Conto
    Jun 2, 2019 at 12:37
  • check this out stackoverflow.com/questions/53882985/… you have the same issue, you need a setter and getter
    – Hameed
    Jun 2, 2019 at 12:45
  • Hameed - that's it. Thanks a lot, got me stuck for a whole day and it was something as simple as that. Answer the question and I'll note it.
    – Conto
    Jun 2, 2019 at 16:04

1 Answer 1

2

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; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.