c# - 查看帖子表单返回null?

标签 c# .net asp.net-core asp.net-core-mvc

我在使用表单返回模型时遇到问题。

问题是当我提交表单时,即使我指定返回模型,值也为空

这是我的 Controller

/image/7P5uY.png

这是我的 View ,返回 null。

@model MyEnglishDictionary.Models.Dictionary
@{
    ViewData["Title"] = "Create";
}
<h2>Create</h2>
<form method="post" asp-action="Create">
    <div class="p-4 border rounded">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row">
            <div class="col-2">
                <label asp-for="Word"></label>
            </div>
            <div class="col-5">
                <input asp-for="Word" class="form-control" />
            </div>
            <span asp-validation-for="Word" class="text-danger"></span>
        </div>

        <div class="form-group row">
            <div class="col-2">
                <label asp-for="Meaning"></label>
            </div>
            <div class="col-5">
                <input asp-for="Meaning" class="form-control" />
            </div>
            <span asp-validation-for="Meaning" class="text-danger"></span>
        </div>

        <div class="form-group row">
            <div class="col-2">
                <label asp-for="Pronunciation"></label>
            </div>
            <div class="col-5">
                <input asp-for="Pronunciation" class="form-control" />
            </div>
            <span asp-validation-for="Pronunciation" class="text-danger"></span>
        </div>
        <br />
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Create" />
            <a asp-action="Index" class="btn btn-success">Back To List</a>
        </div>
    </div>
</form>
@section Scripts{
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

编辑

这是我的字典 Controller 。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MyEnglishDictionary.Data;
using MyEnglishDictionary.Models;

namespace MyEnglishDictionary.Controllers
{
    public class DictionaryController : Controller
    {
        private readonly ApplicationDbContext _db;

        public DictionaryController(ApplicationDbContext db)
        {
            _db = db;
        }


        public IActionResult Index()
        {
            return View(_db.Dictionaries.ToList());
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(Models.Dictionary word)
        {
            if(!ModelState.IsValid)
            {
                return View(word);
            }

            _db.Add(word);
            await _db.SaveChangesAsync();

            return RedirectToAction(nameof(Index));
        }
    }
}

这是我的字典模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyEnglishDictionary.Models
{
    public class Dictionary
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Word { get; set; }
        [Required]
        public string Meaning { get; set; }
        [Required]
        public string Pronunciation { get; set; }
        public string Link { get; set; }
        public DateTime Date { get; set; }
    }
}

我正在使用 Net Core 2.1,但我有一些项目使用相同的方式将表单模型从 View 传递到 Controller ,并且它们可以工作。

最佳答案

需要注意参数名称和字段。

对于您的问题,这是由于您定义了一个字段 Word 引起的参数为word导致绑定(bind)失败。

尝试更改public async Task<IActionResult> Create(Models.Dictionary word)public async Task<IActionResult> Create(Models.Dictionary dictionary) .

关于c# - 查看帖子表单返回null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976474/

相关文章:

c# - 如何从 C# 调用混合模式 C++/CLI 程序集?

c# - 将用户传递给 ViewModel

c# - .NET 中属性的性能开销

c# - 如何检测单击 Windows 窗体中的一行

asp.net-core - NavigateTo 上的 NavigationError

c# - WCF 和 ref 参数

c# - WCF:通用接口(interface)的序列化是否可能?

c# - 使用 AppDomain 临时加载程序集

c# - .NET Core 2.0 带路由的最小 WebHost

c# - 使用 EF Core 时异步调用比同步调用慢