【问题标题】:View post form return null?查看帖子表单返回null?
【发布时间】:2019-03-04 04:06:40
【问题描述】:

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

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

这是我的控制器

这是我返回 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"); }
}

编辑

这是我的字典控制器。

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,但我有一些项目使用相同的方式将表单模型从视图传递到控制器并且它们可以工作。

【问题讨论】:

  • 可能你没有为你的模型添加绑定属性[BindProperty],你能把你的控制器发布到问题中而不是图片。
  • 你的@Html.AntiForgeryToken() 方法在哪里?
  • @FatikhanGasimov 使用 Asp.Net Core 2 或更高版本,表单标签会自动将防伪令牌注入 html 表单元素,请参阅文档 here
  • 您应该在答案中发布您的控制器代码而不是屏幕截图,这对于试图帮助您的人来说更难
  • 可以加你Dictionary类吗?

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


【解决方案1】:

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

对于您的问题,是由于您定义了一个字段为Word,而参数为word,导致绑定失败。

尝试将public async Task&lt;IActionResult&gt; Create(Models.Dictionary word) 更改为public async Task&lt;IActionResult&gt; Create(Models.Dictionary dictionary)

【讨论】:

    【解决方案2】:

    word 参数名称更改为_word 之类的其他名称,编译器似乎不接受它作为c# 中的参数名称。

    public async Task<IActionResult> Create(Models.Dictionary _word)
    {
        if(!ModelState.IsValid)
        {
            return View(_word);
        }
    
        _db.Add(_word);
        await _db.SaveChangesAsync();
    
        return RedirectToAction(nameof(Index));
    }
    
    

    顺便说一句,我在保留关键字list中没有看到它

    【讨论】:

      【解决方案3】:

      您必须将属性绑定到您的模型。在你的情况下:

      public async Task<IActionResult> Create([Bind("Word, Meaning, Pronounciation")] Dictionary word)
      

      进一步阅读: Model Binding

      【讨论】:

      • 使用 .Net Core,您不必添加 @Html.AntiForgeryToken(),因为它会自动注入到表单标签中,请参阅 documentation 了解更多详情
      • @LazZiya 谢谢,不知道。
      猜你喜欢
      • 2020-04-27
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      相关资源
      最近更新 更多