TryUpdateModel Asp.Net MVC不工作 [英] TryUpdateModel Asp.Net MVC is not working
本文介绍了TryUpdateModel Asp.Net MVC不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想学习如何使用TryUpdateModel但我无法得到它的工作,你可以在下面找到我的code:
控制器侧
块引用>使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;命名空间EFW6.Controllers
{ 公共类HomeController的:控制器
{
//
// 回家/
私人WorkFlowContext语境=新WorkFlowContext();
公众的ActionResult指数()
{ 返回查看();
} [HttpPost]
公共字符串UploadFile(的FormCollection形式)
{
文件的文件=新的文件(); 如果(TryUpdateModel(文件,form.ToValueProvider()))
{
返回True+ file.filePath;
}
其他
{
返回假;
} }
}
}
查看边
块引用>@ {
ViewBag.Title =指数;
}< H2> @模型与LT; / H><形式方法=邮报行动=首页/ UploadFile>
<输入类型=文本名称=文件路径>
<输入类型=提交>
< /表及GT;
模型类
块引用>类文件
{
公共字符串文件路径;
}虽然它返回true值作为操作的结果,当我返回文件路径的值返回什么。
解决方案的问题是,你我在的文件的类
您必须改变它是这样
类文件
{
公共字符串的文件路径{搞定;组; }
}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; } }
这篇关于TryUpdateModel Asp.Net MVC不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文