簡體   English   中英

ASP.NET MVC HttpPost 異步任務無法正常運行

[英]ASP.NET MVC HttpPost async task not functioning correctly

我正在嘗試在我的演員頁面中創建一個表單。 單擊提交按鈕后,字段將被驗證,理論上應該提交,但事實並非如此。 我已經嘗試重命名,創建一個新的 function,這是 intellisense 建議的,而我提交此表單的唯一方法是手動將 function go 設為 _service.Add(actor); 或者不進行驗證,但是如果不滿足其中一個必填字段,它會在不同的頁面中拋出錯誤,這是不理想的。 我不知道如何完成這項工作,因為我從中重新創建它的課程能夠很好地完成它。

我的代碼 - controller:

namespace Cinema_World.Controllers
{
    public class ActorsController : Controller
    {
        private readonly IActorsService _service;

        public ActorsController(IActorsService service)
        {
            _service = service;
        }

        public async Task<IActionResult> Index()
        {
            var allActors = await _service.GetAll();
            return View(allActors);
        }

        public async Task<IActionResult> Create()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create([Bind("FirstName,MiddleName,LastName,BirthYear,BirthPlace")] ActorModel actor)
        {
            if (!ModelState.IsValid) //when i use break-points, this part gets stepped into
            {// also this part
                return View(actor); //this part too
            } // and this is the final part, then it skips to the end and nothing happens in the browser
            _service.Add(actor);
            return RedirectToAction(nameof(Index));
        }
    }
}

我的模特:

namespace Cinema_World.Models
{
    public class ActorModel
    {
        [Key]
        public int ActorID { get; set; }

        [Display(Name = "First name")]
        [Required(ErrorMessage = "First name is a required field")]
        [StringLength(100, MinimumLength = 1, ErrorMessage = "First name can be between 1 and 100 characters long!")]
        public string FirstName { get; set; }

        [Display(Name = "Middle name")]
        [StringLength(100, MinimumLength = 1, ErrorMessage = "Middle name can be between 1 and 100 characters long!")]
        public string? MiddleName { get; set; }

        [Display(Name = "Last name")]
        [Required(ErrorMessage = "Last name is a required field")]
        [StringLength(100, MinimumLength = 1, ErrorMessage = "Last name can be between 1 and 100 characters long!")]
        public string LastName { get; set; }

        [Display(Name = "Year of Birth")]
        [Required(ErrorMessage = "Year of birth is a required field")]
        [Range(999,9999, ErrorMessage = "Input a year between 999 and 9999")]
        public int BirthYear { get; set; }

        [Display(Name = "Place of Birth")]
        [Required(ErrorMessage = "Place of birth is a required field")]
        [StringLength(100, MinimumLength = 1, ErrorMessage = "Name of the place can be between 1 and 100 characters long!")]
        public string BirthPlace { get; set; }

        public  List<Actor_CinematographyModel> Actors_Cinematography { get; set; }
    }
}

當表單提交成功時,來自我的服務的代碼被調用。

namespace Cinema_World.Data.Services
{
    public class ActorsService : IActorsService
    {
        private readonly ApplicationDbContext _context;

        public ActorsService(ApplicationDbContext context)
        {
            _context = context;
        }

        public void Add(ActorModel Actor)
        {
            _context.Actors.Add(Actor);
            _context.SaveChanges();
        }

        public void Delete(int ActorID)
        {
            throw new NotImplementedException();
        }

        public async Task<IEnumerable<ActorModel>> GetAll()
        {
            var result = await _context.Actors.ToListAsync();
            return result;
        }

        public ActorModel GetById(int ActorID)
        {
            throw new NotImplementedException();
        }

        public ActorModel Update(int ActorID, ActorModel newActor)
        {
            throw new NotImplementedException();
        }
    }
}

此特定服務的接口:

namespace Cinema_World.Data.Services
{
    public interface IActorsService
    {
        Task<IEnumerable<ActorModel>> GetAll();
        ActorModel GetById(int ActorID);
        void Add(ActorModel Actor);
        ActorModel Update(int ActorID, ActorModel newActor);
        void Delete(int ActorID);
    }
}

查看標記:

<div class="row text">
    <div class="col-md-8 offset-2">
        <p>
            <h1>Add a new Actor!</h1>
        </p>

        <div class="row">
            <div class="col-md-8 offset-2">

                <form asp-action="Create">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>

                    <div class="form-group">
                        <label asp-for="FirstName" class="control-label"></label>
                        <input asp-for="FirstName" class="form-control" />
                        <span asp-validation-for="FirstName" class="text-danger"></span>
                    </div>

                    <div class="form-group">
                        <label asp-for="MiddleName" class="control-label"></label>
                        <input asp-for="MiddleName" class="form-control" />
                        <span asp-validation-for="MiddleName" class="text-danger"></span>
                    </div>

                    <div class="form-group">
                        <label asp-for="LastName" class="control-label"></label>
                        <input asp-for="LastName" class="form-control" />
                        <span asp-validation-for="LastName" class="text-danger"></span>
                    </div>

                    <div class="form-group">
                        <label asp-for="BirthYear" class="control-label"></label>
                        <input asp-for="BirthYear" class="form-control" />
                        <span asp-validation-for="BirthYear" class="text-danger"></span>
                    </div>

                    <div class="form-group">
                        <label asp-for="BirthPlace" class="control-label"></label>
                        <input asp-for="BirthPlace" class="form-control" />
                        <span asp-validation-for="BirthPlace" class="text-danger"></span>
                    </div>

                    <div class="form-group mt-2">
                        <input type="submit" value="Create" class="btn btn-outline-success float-end"/>
                        <a class="btn btn-outline-dark" asp-action="Index">Show all</a>
                    </div>

                </form> 

            </div>
        </div>
    </div>
</div>

如果還有其他需要,請告訴我。 我已經為此苦苦掙扎了一段時間,如果我不能解決這個問題,我的另一個 forms 就無法取得進展。

就像我之前說的,我嘗試用ModelState.IsValid替換.ModelState.IsValid並將執行代碼放在那里,智能感知建議甚至手動使用斷點,我確實設法讓它像那樣工作,但不是一個理想的選擇。

我在 ASP.NET MVC 中的知識是基本的,所以也許我搞砸了某些東西或遺漏了某些東西。

只是為了澄清 - 被調用的服務有效,我能夠發布數據,但如果驗證以相同的方法存在,我將無法發布任何內容並且按鈕不執行任何操作。

事實證明,問題不在方法中,而在 model.public public List<Actor_CinematographyModel> Actors_Cinematography { get; set; } public List<Actor_CinematographyModel> Actors_Cinematography { get; set; } public List<Actor_CinematographyModel> Actors_Cinematography { get; set; }不知何故相互矛盾,不允許我發布數據。 在使成為 null 值成為可能之后,一切正常。 不確定,為什么會這樣,也許我設置了錯誤的數據庫表。 這個線程不再有問題:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM