【发布时间】:2026-01-30 00:25:01
【问题描述】:
我正在使用 Framework 4.5,并决定利用它的一些改进.. 假设我在 FormView 中有以下控件:
<asp:TextBox ID="EventNumberTextBox" runat="server" Text='<%# Item.Event_Num %>' />
在我的 UpdateMethod 中,我做了以下操作:
public void UpdateModelFromView(int? ID)
{
var modelView = new AlertMessageController().LoadById(ID);
var updateSuccess = TryUpdateModel(modelView);
if (updateSuccess)
{
new AlertMessageController().Save(modelView);
RedirectToViewPage();
}
}
问题在于,尽管 Form 集合确实包含我所做的所有更改——在给定的情况下,我已将 Event_Num 设置为某个值——但这些更改并未反映到 modelView。换句话说,我对 FormView 控件所做的任何更改都将被忽略,并且模型在加载后按原样保存...
【问题讨论】:
-
您是否尝试过在 TextBox 上使用 BindItem 而不是 Item,即 BindItem.Event_Num?
-
格雷厄姆,非常感谢,我觉得我快到了,但现在又遇到了另一个问题。问题是我必须只从表单中获取字段并忽略 URL。当我只使用一个参数的 TryUpdateModel 时 - 它使用 URL 和表单参数更新模型。我尝试使用第二个参数:new NameValueCollectionValueProvider(HttpContext.Current.Request.Form, Culture.GetBrowserOrDefaultCulture()) 或 new FormValueProvider(Page.ModelBindingExecutionContext),但在这些情况下 TryUpdateModel 会完全忽略所有更改的值...
-
我已尝试更改 UpdateMethod 的参数以接受完整对象:UpdateModelForView(AlertMessageInfo item)。现在 ModelState 似乎包含表单中的所有键,但是......当我尝试使用 TryUpdateModel(modelView, new FormValueProvider(Page.ModelBindingExecutionContext)) 时,它的值未应用
-
太好了,你正在取得进展。 Web 窗体数据绑定不从 Url 获取值(只有 MVC 数据绑定会这样做)。 Web 窗体数据绑定只会从指定了 BindItem 的 FormView 控件中获取值。没有看到更多你的代码,我不知道到底出了什么问题。
-
现在可以使用 BindItem 而不是 Item 和 TryUpdateModel(modelView) - 只有一个参数。如此简单:) 但是现在我很好奇,为什么当我使用第二个参数作为 new FormValueProvider(Page.ModelBindingExecutionContext) 时它不起作用?