0

I'm using the latest preview of Asp.Net Core. I have a page with pretty simple edit form and some additional data displayed.

@model SeasonFullDetailsViewModel

.....

<form asp-action="Save">
    <div class="form-horizontal">
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <input type="hidden" asp-for="SeasonId" />
        <div class="form-group">
            <label asp-for="Season" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Season" class="form-control" />
                <span asp-validation-for="Season" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="IsFinal" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="IsFinal" class="form-control" />
                <span asp-validation-for="IsFinal" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

The problem is that I can't get form data to be submitted into a model, only as separate fields.

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Save(int seasonId, string season, bool isFinal)
{
    // values are ok
    return new EmptyResult();
}

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Save(SeasonFullDetailsViewModel season)
{
    // empty values inside season
    return new EmptyResult();
}

I think the problem is that model uses inheritance and data from the form is contained in parent classes.

public class SeasonFullDetailsViewModel : SeasonDetailsViewModel
{
    // some data here
}


public class SeasonDetailsViewModel : SeasonBaseViewModel
{   
    public bool IsFinal { get; set; }

    // also some other data
}


public class SeasonBaseViewModel
{
    public int SeasonId { get; set; }

    public string Season { get; set; }

    // and some more data
}

I have tried to use a simpler model, but it doesn't work either.

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Save(SeasonEditModel season)
{
    // empty values inside season
    return new EmptyResult();
}

public class SeasonEditModel
{
    public int SeasonId { get; set; }

    public string Season { get; set; }

    public bool IsFinal { get; set; }
}

I know I can use Ajax to send data or just create the needed model from separate fields, but I want to find out what is the problem here and if there is a way to overcome it. Why the solution with SeasonEditModel doesn't work?

Sergi0
  • 1,084
  • 14
  • 28
  • Possible duplicate of [Asp.Net MVC: Why is my view passing NULL models back to my controller?](https://stackoverflow.com/questions/34863167/asp-net-mvc-why-is-my-view-passing-null-models-back-to-my-controller) –  Sep 30 '17 at 21:25

2 Answers2

3

This was answered here: asp.Net MVC view model is empty on post

By convention, you need to use the same parameter name as the view model name. You have:

public IActionResult Save(SeasonFullDetailsViewModel season)

If you change the parameter name to match like this, it should work:

public IActionResult Save(SeasonFullDetailsViewModel seasonFullDetailsViewModel)
Community
  • 1
  • 1
Justin Saraceno
  • 2,516
  • 1
  • 18
  • 16
  • 1
    it would be nice to at least see a debug message that model named 'seasonFullDetailsViewModel' wasn't found. it helped, thanks. actually I can choose any model type as long as it has the right name. – Sergi0 Jun 02 '16 at 22:19
  • 3
    This is wrong. You do **NOT** _need to use the same parameter name as the view model name_. It just cannot be the same name as a property in the model. –  Sep 30 '17 at 21:25
1

I believe Justin's answer is correct with one point of distinction: that model variable naming convention only comes into play when you are posting data to the controller action via an HTML form post.

The same is not true if you posted data to the action by manually building your own post data structure and not via a form collection.
ex. an AJAX call.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
Eric Davis
  • 101
  • 4