表单未提交点击

时间:2019-10-21 01:29:27

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

我试图在我的一个控制器(LeagueController)中添加一个“创建”,所以我创建了一个Create视图,该视图使用了我的League对象。

但是,当我提交表单时,它并没有按我希望的那样重定向回Index视图,也没有输入日志条目来说明我创建了一个联赛。

League

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; }
}

LeaguesController

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>

2 个答案:

答案 0 :(得分:1)

在您的表单Last word : sentence Remaining words : This is a 中添加标记以指向正确的控制器<form asp-action="Create" method="post">

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

答案 1 :(得分:1)

第一种方法是,您可以删除get方法上的[Route("Create")]

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

[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");
}