【问题标题】:After submitting the form,I received null value in the controller提交表单后,我在控制器中收到空值
【发布时间】:2020-03-06 16:13:28
【问题描述】:

当我提交表单时,我在控制器中得到 null 值。(asp.net core 3.x) 我该如何解决这个问题?

@model Alpha.ViewModels.ArticleViewModel

@{
    ViewData["Title"] = "View";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

<div class="col-12">
        @using (Html.BeginForm("Edit", "Article", new {area = "Admin"}, FormMethod.Post))
        {
            
            @*<div asp-validation-summary="ModelOnly" class="text-danger"></div>*@

            <div class="form-group">
                <label asp-for="Article.Title" class="control-label"></label>
                <input asp-for="Article.Title" class="form-control"/>
                <span asp-validation-for="Article.Title" class="text-danger"></span>
            </div>
            
             <div class="form-group">
                <input type="submit" value="Save changes" class="btn btn-default"/>
            </div>
        }
    </div>
    [HttpPost]
    public async Task<IActionResult> Edit(ArticleViewModel articleViewModel)
    {// articleViewModel.Article is null  why??????
        if (ModelState.IsValid)
        {

        }
        return View(articleViewModel);
    }

视图模型:

namespace Alpha.ViewModels
{
public class ArticleViewModel : BaseViewModel
{
    public Article Article { get; set; }

    /// <summary>
    /// Just only related tags
    /// </summary>
    public List<Tag> Tags { get; set; }

    /// <summary>
    /// Related tags in the set of all tags
    /// </summary>
    public List<Tag> AllTags { get; set; }
}
}

【问题讨论】:

    标签: asp.net asp.net-core razor


    【解决方案1】:

    我刚刚将Microsoft.AspNetCore.Mvc.Core 包安装到我的项目中,然后一切正常!

    安装包 Microsoft.AspNetCore.Mvc.Core -Version 2.2.5

    https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Core/

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      相关资源
      最近更新 更多