2

I know, there's a lot of questions, tried the solutions, neither of them worked. Basically, I'm stuck...

ViewModel:
public class CreateStrengthViewModel
{
    public string Strength { get; set; }
    public IEnumerable<Categories.CategoryViewModel> Categories { get; set; }
    public int Category { get; set; }
}

Post:
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="__RequestVerificationToken"
iErDomlK5vCWAFFMlGkb2-HgLCoquxfeIlYeI3pmrsW_5VSD8-huS6JbCdA4OAg4s8nMgqKAHPHArVoQ3GzfFWf2I-Yx6iWgvkWRNI6jiKA1
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="Strength"
FMC Extra
-----------------------------7e01e1381a08c2
Content-Disposition: form-data; name="Category"
1
-----------------------------7e01e1381a08c2--

Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
    [Bind(Include = "Strength,Category")]
    CreateStrengthViewModel strength)
    {
        using (StrengthServices service = new StrengthServices(new ImperialTobacco_Database()))
        {
            //service.CreateAndSave(strength);
            ...
        }
    }
}

The strength inside the controller method is null whatever I'm doing. The browser sends the data as you can see, but the ModelBinder just won't do a thing. Oh, and by the way: The very same code works for a different ViewModel with different Include fields. Yeah, I've tried to remove the Bind, or bind just one of the incoming variables nothing changes.

Funny thing is; this is working:

public class CreateCompanyViewModel
{
    public string Name { get; set; }
    public string Color { get; set; }

    public IEnumerable<MarketViewModel> MarketsOfOperations { get; set; }
    public IEnumerable<int> SelectedOperations { get; set; }
}

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Name,Color,SelectedOperations")] CreateCompanyViewModel company)
    {
        using (CompanyServices service = new CompanyServices(new ImperialTobacco_Database()))
        {
            service.CreateAndSave(company);
            return RedirectToAction("Index", "Management");
        }
    }

I'll be honest, I have no idea what's going on and why the ModelBinder picks up nothing from the posted data, especially as a nearly identical one works perfectly.

P.S.: I tried changing the Property and Bind names yet again no luck.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Joseph
  • 85
  • 6
  • Possible duplicate of [Asp.Net MVC: Why is my view passing NULL models back to my controller?](http://stackoverflow.com/questions/34863167/asp-net-mvc-why-is-my-view-passing-null-models-back-to-my-controller) –  Apr 14 '16 at 10:40

1 Answers1

0

The issue is you have a property name in your model that matches the argument name in your action method.

So if you change the action name argument from

public ActionResult Create([Bind(Include = "Strength,Category")]  CreateStrengthViewModel strength)

To

public ActionResult Create([Bind(Include = "Strength,Category")]  CreateStrengthViewModel strengthModel)

That will resolve it

Slicksim
  • 7,054
  • 28
  • 32