Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 点击后不提交表格_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 点击后不提交表格

C# 点击后不提交表格,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我试图向我的一个控制器(LeagueController)添加一个“创建”,因此我创建了一个Create视图,它使用我的League对象 但是,当我去提交表单时,它不会像我希望的那样重定向回索引视图,也不会输入日志条目来表示我创建了一个联盟 LeagueClass public class League : BaseEntity { [Required] [DataType(DataType.Text)] public string LeagueName { get; s

我试图向我的一个控制器(
LeagueController
)添加一个“创建”,因此我创建了一个
Create
视图,它使用我的
League
对象

但是,当我去提交表单时,它不会像我希望的那样重定向回
索引
视图,也不会输入日志条目来表示我创建了一个联盟

League
Class

public class League : BaseEntity
{
    [Required]
    [DataType(DataType.Text)]
    public string LeagueName { get; set; }

    [Required]
    [DataType(DataType.Text)]
    public string LeagueInitials { get; set; }

    [DataType(DataType.Text)]
    public string LeagueURL { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime Founded { get; set; }

    [InverseProperty("League")]
    public ICollection<Team> Teams { get; set; }

    [ForeignKey("LeagueID")]
    public ICollection<LeagueOwners> LeagueOwners { get; set; }
}
public class LeaguesController : Controller
{
    private MyDBContext context;
    private ILogger logger;

    public LeaguesController(MyDBContext context, ILogger logger)
    {
        this.context = context;
        this.logger = logger;
    }

    public IActionResult Index()
    {
        this.logger.LogInformation("Reached League Index");
        return View();
    }

    [Route("Create")]
    public IActionResult Create()
    {
        this.logger.LogInformation("Creating a league");
        return View();
    }

    [HttpPost]
    public IActionResult Create(League league)
    {
        this.logger.LogInformation("Create button clicked!");
        return this.RedirectToAction("Index");
    }
}
Create.cshtml

@model MySite.Core.Entities.League

@{
    ViewData["Title"] = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

<h4>League</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="LeagueName" class="control-label"></label>
                <input asp-for="LeagueName" class="form-control" />
                <span asp-validation-for="LeagueName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LeagueInitials" class="control-label"></label>
                <input asp-for="LeagueInitials" class="form-control" />
                <span asp-validation-for="LeagueInitials" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LeagueURL" class="control-label"></label>
                <input asp-for="LeagueURL" class="form-control" />
                <span asp-validation-for="LeagueURL" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Founded" class="control-label"></label>
                <input asp-for="Founded" class="form-control" />
                <span asp-validation-for="Founded" class="text-danger"></span>
            </div>
            <div class="form-group" hidden="hidden">
                <label asp-for="Created" class="control-label"></label>
                <input asp-for="Created" class="form-control" value="@DateTime.Now" />
                <span asp-validation-for="Created" class="text-danger"></span>
            </div>
            <div class="form-group" hidden="hidden">
                <label asp-for="Modified" class="control-label"></label>
                <input asp-for="Modified" class="form-control" value="@DateTime.Now" />
                <span asp-validation-for="Modified" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>
@model MySite.Core.Entities.League
@{
ViewData[“标题”]=“创建”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创造
联盟

返回列表
在表单中添加一个标记,指向正确的控制器
asp controller=“Leagues”

不要强制执行路由,这只是反模式。

在表单中添加一个标记以指向正确的控制器
asp controller=“Leagues”


不要强制路由,这只是反模式。

第一种方法是可以删除get方法中的
[route(“Create”)]

第二种方法是,您可以向post方法添加
[Route]
属性,如下所示:

[Route("Create")]
public IActionResult Create()
{
    this.logger.LogInformation("Creating a league");
    return View();
}
[Route("Create")]
[HttpPost]
public IActionResult Create(League league)
{
    this.logger.LogInformation("Create button clicked!");
    return this.RedirectToAction("Index");
}

第一种方法是可以删除get方法中的
[Route(“Create”)]

第二种方法是,您可以向post方法添加
[Route]
属性,如下所示:

[Route("Create")]
public IActionResult Create()
{
    this.logger.LogInformation("Creating a league");
    return View();
}
[Route("Create")]
[HttpPost]
public IActionResult Create(League league)
{
    this.logger.LogInformation("Create button clicked!");
    return this.RedirectToAction("Index");
}

它是否击中了功能?你试过放一个断点吗?还可以查找CSRF cookies和表单值,因为它是HTTP POST。请在公共IActionResult Create(){this.logger.LogInformation(“创建联盟”);return View();}上提及HTTP GET,也可以在POST操作上路由。它是否命中了函数?你试过放一个断点吗?还可以查找CSRF cookies和表单值,因为它是一个HTTP POST。请在公共IActionResult Create(){this.logger.LogInformation(“创建联盟”);return View();}上提及HTTP GET也可以在POST操作上路由。谢谢!我直接添加了路由。它只是看起来更干净,更容易遵循。谢谢!我直接添加了路由。它只是看起来更干净,更容易遵循