远程验证因IFormFile属性而失败

时间:2018-09-19 01:34:00

标签: asp.net-core-mvc asp.net-core-2.1

我注意到ASP.NET Core中的远程验证始终失败,因为服务器在控制器方法中始终收到一个空的IFormFile。有没有办法使其工作?一些代码可以重现该问题:

包含了模型类(“未映射”,因此Entity Framework不会产生干扰,但是在没有Entity Framework的其他项目中也无法使用。

for link in soup.findAll('a', {'rel':'bookmark'} ): 
href= link.get('href')                          
title=link.get('title')                         

控制器

public class Movie
{
    public int ID { get; set; }
    [Sacred(sacredWord: "sonda")]
    public string Title { get; set; }

    [Display(Name = "Release Date")]
    [DataType(DataType.Date)]
    public DateTime ReleaseDate { get; set; }
    public string Genre { get; set; }

    [Column(TypeName = "decimal(18, 2)")]
    public decimal Price { get; set; }

    [Remote(action: "VerifyRating", controller: "Movies")]
    public string Rating { get; set; }

    [NotMapped]
    [Remote(action: "VerifyFile", controller: "Movies"),Required]
    public IFormFile File { get; set; }
}

和视图

public class MoviesController : Controller
{
    private readonly WebAppMVCContext _context;

    public MoviesController(WebAppMVCContext context)
    {
        _context = context;
    }



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

    [AcceptVerbs("Get", "Post")]
    public IActionResult VerifyFile(IFormFile File)
    {
        if(File == null)
        {
            return Json("The file is null");

        }
        else
        {
            return Json("The file is not null");
        }
    }


    // POST: Movies/Create
    // To protect from overposting attacks, please 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,Title,ReleaseDate,Genre,Price,Rating")] Movie movie)
    {
        if (ModelState.IsValid)
        {
            _context.Add(movie);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(movie);
    }       

    [AcceptVerbs("Get", "Post")]
    public IActionResult VerifyRating( int rating)
    {
        if(rating>0 && rating < 10)
        {
            return Json(true);
        }
        else
        {
            return Json($"The rating is invalid");
        }
    }

请注意,所有其他验证均有效(包括远程验证“ VerifyRating”)。

0 个答案:

没有答案