asp.net-mvc – UpdateModel前缀 – ASP.NET MVC

我在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与IIS是紧密联系的,由于IIS6.0与IIS7.0的工作方式的...
在之前的ASP.NET是如何在IIS下工作的这篇文章中介绍了ASP.NE...
这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...