asp.net-mvc – UpdateModel前缀 – ASP.NET MVC
发布时间:2020-12-30 20:51:44  所属栏目:asp.Net  来源:互联网 
            导读:我在TryUpdateModel()中遇到麻烦.我的表单字段用前缀命名,但我正在使用 – 作为我的分隔符,而不是默认点. input type=text id=Record-Title name=Record-Title / 当我尝试更新模型时,它不会被更新.如果我将name属性更改为Record.Title,它的工作原理完美,但这
                
                
                
            | 
                         我在TryUpdateModel()中遇到麻烦.我的表单字段用前缀命名,但我正在使用 – 作为我的分隔符,而不是默认点. <input type="text" id="Record-Title" name="Record-Title" /> 当我尝试更新模型时,它不会被更新.如果我将name属性更改为Record.Title,它的工作原理完美,但这不是我想要做的. bool success = TryUpdateModel(record,"Record"); 是否可以使用自定义分隔符? 解决方法另外需要注意的是前缀是帮助反射找到正确的字段进行更新.例如,如果我有一个我的ViewData的自定义类,如:public class Customer
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
}
public class MyCustomViewData
{
    public Customer Customer {get; set;}
    public Address Address {get; set;}
    public string Comment {get; set;}
} 
 我的页面上有一个文本框 <%= Html.TextBox("FirstName",ViewData.Model.Customer.FirstName) %> 
 要么 <%= Html.TextBox("Customer.FirstName",ViewData.Model.Customer.FirstName) %> 
 这是有用的 public ActionResult Save (Formcollection form)
{
    MyCustomViewData model = GetModel(); // get our model data
    TryUpdateModel(model,form); // works for name="Customer.FirstName" only
    TryUpdateModel(model.Customer,form) // works for name="FirstName" only
    TryUpdateModel(model.Customer,"Customer",form); // works for name="Customer.FirstName" only
    TryUpdateModel(model,form) // do not work
    ..snip..
}                        (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
- asp.net-mvc – 从基本控制器继承LINQ-to-SQL数据上下文
 - .NET WebApi jsonapi.org支持
 - ASP.NET中读取XML文件信息的4种方法与示例代码
 - asp.net – 将appsettings.production.json发布到azure上
 - asp.net-mvc – 类库中的控制器和视图
 - asp.net-web-api – 从ASP.NET Web API ASP.NET Core 2返回
 - asp.net – 如何使一个TextBox控件是多行不可调整大小?
 - asp.net-mvc – 在没有模型的情况下手动将验证添加到文本框
 - asp.net-mvc – 在ASP.NET身份中角色与声明的最佳实践
 - 谈谈.NET Core中基于Generic Host来实现后台任务
 
推荐文章
            站长推荐
            - 深蓝词库转换1.6发布
 - asp.net-mvc – 为什么HttpContext.Current在asp
 - 使用System.Net.Mail中的SMTP发送邮件(带附件)
 - asp.net-mvc – URL中的ASP.NET MVC冒号
 - asp.net – UserControl Viewstate在回发后丢失所
 - asp.net – 如何使用Inno Setup脚本创建IIS应用程
 - 如何设置特定于ASP.NET请求的log4net上下文属性?
 - asp.net – 使用app_offline.htm使应用程序脱机,
 - ASP.Net下载大文件的实现方法
 - asp.net-mvc – 如何使用ASP.NET MVC ApiControl
 
热点阅读
            