简体   繁体   English

TryUpdateModel Asp.Net MVC无法正常工作

[英]TryUpdateModel Asp.Net MVC is not working

I am trying to learn how to use TryUpdateModel but I cannot get it to work, you can find my code below: 我正在尝试学习如何使用TryUpdateModel,但无法正常工作,您可以在下面找到我的代码:

Controller Side 控制器端

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

        }


    }
}

View Side 查看侧

@{
    ViewBag.Title = "index";
}

<h2>@Model</h2>

<form method="post" action="Home/UploadFile">
    <input type="text" name="filePath">
    <input type="submit">
</form>

Model Class 型号类别

class Files
{
    public string filePath;
}

When I return the value of the file path it returns nothing while it returns the value True for as a result for the operation. 当我返回文件路径的值时,它不返回任何内容,而作为操作的结果,它返回值True。

the problem is that you I am using field instead of property in the Files Class 问题是您正在使用字段而不是文件类中的属性

You have to change it to be like this 您必须将其更改为这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM