简体   繁体   中英

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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