微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET核心“创建”操作

如何解决ASP.NET核心“创建”操作

我正在使用asp.net mvc创建一个Web应用程序。 我使用认的脚手架来创建控制器和视图(我已经预先创建了数据库用户表和游戏表) 当我尝试使用认创建表单在游戏表中创建新实体时,它不会提交数据。 当我在用户表中执行相同操作时,效果很好。

问题是它是Visual Studio生成代码,我无法弄清楚是什么原因。

这是控制器创建操作:

        // GET: Games/Create
    public IActionResult Create()
    {
        return View();
    }

    // POST: Games/Create
    // To protect from overposting attacks,enable the specific properties you want to bind to,for 
    // more details,see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("ID,Name,ReleaseDate,Genre,ageRestriction,Developer,Description")] Game game)
    {
        if (ModelState.IsValid)
        {
            _context.Add(game);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(game);
    }

这是创建视图:

@model GameSense.Models.Game

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Game</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ReleaseDate" class="control-label"></label>
                <input asp-for="ReleaseDate" class="form-control" />
                <span asp-validation-for="ReleaseDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Genre" class="control-label"></label>
                <input asp-for="Genre" class="form-control" />
                <span asp-validation-for="Genre" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ageRestriction" class="control-label"></label>
                <input asp-for="ageRestriction" class="form-control" />
                <span asp-validation-for="ageRestriction" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Developer" class="control-label"></label>
                <input asp-for="Developer" class="form-control" />
                <span asp-validation-for="Developer" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Description" class="control-label"></label>
                <input asp-for="Description" class="form-control" />
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

这是“游戏”的模型:

using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace GameSense.Models
{
    public class Game
    {
        public int ID { get; set; }
        [required]
        public string Name { get; set; }
        [required]
        [DataType(DataType.Date)]
        public DateTime ReleaseDate { get; set; }
        [required]
        public string Genre { get; set; }
        [required]
        public int ageRestriction { get; set; }
        [required]
        public string Developer { get; set; }
        [required]
        public Coordinates DeveloperLocation { get; set; }
        [required]
        public string Description { get; set; }
    }
}
就像我说的那样,

非常“认”,我只想看到一切都可以继续进行。 有什么想法,为什么我不能使用认格式在数据库中创建新的“游戏”项目?

解决方法

您已在模型中将Coordinates属性设置为Required,但是您并没有要求用户在表单中为此输入任何值。因此,它将始终为null(因此始终会失败验证)。

您可以从属性中删除[Required]属性,或者从模型中完全删除该属性,或者构建一些UI以允许用户填充该字段。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。