empty model submitted from the form

▼魔方 西西 提交于 2019-12-12 02:18:25

问题


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?


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/37595201/empty-model-submitted-from-the-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!