TryUpdateModel Asp.Net MVC不起作用

本文关键字:不起作用 MVC Net Asp TryUpdateModel | 更新日期: 2023-09-27 18:01:18

我试图学习如何使用TryUpdateModel,但我不能让它工作,你可以在下面找到我的代码:

控制器侧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace EFW6.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private WorkFlowContext context = new WorkFlowContext();
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public string UploadFile(FormCollection form)
        {
            Files file = new Files();
            if (TryUpdateModel(file, form.ToValueProvider()))
            {
                return "True " + file.filePath;
            }
            else 
            {
                return "False";
            }
        }

    }
}

视图的一面

@{
    ViewBag.Title = "index";
}
<h2>@Model</h2>
<form method="post" action="Home/UploadFile">
    <input type="text" name="filePath">
    <input type="submit">
</form>

模型类

class Files
{
    public string filePath;
}

当我返回文件路径的值时,它不返回任何值,而返回值True作为操作的结果

TryUpdateModel Asp.Net MVC不起作用

问题是我在文件

中使用字段而不是属性

我必须把它改成这样

class Files
{
    public string FilePath { get; set; }
}