gpt4 book ai didi

asp.net-mvc-2 - Html.TextBox 和 Html.TextBoxFor 的区别

转载 作者:行者123 更新时间:2023-12-04 00:40:27 25 4
gpt4 key购买 nike

Html.TextBox 和 Html.TextBoxFor 有什么区别?据我所知,它们产生相同的 html 输出。除非我在这里遗漏了什么。请帮我解决这个问题。

最佳答案

Html.TextBox不是强类型的,它不需要强类型的 View ,这意味着您可以将您想要的任何名称硬编码为第一个参数并为其提供一个值:

<%= Html.TextBox("foo", "some value") %>

您可以在 ViewData 中设置一些值 Controller 操作中的字典,助手将在呈现文本框 ( ViewData["foo"] = "bar" ) 时使用此值。
Html.TextBoxFor is 需要强类型 View 并使用 View 模型:
<%= Html.TextBoxFor(x => x.Foo) %>

助手将使用 lambda 表达式来推断传递给 View 的 View 模型的名称和值。

并且因为使用强类型 View 和 View 模型是一种很好的做法,所以您应该始终使用 Html.TextBoxFor helper 。

关于asp.net-mvc-2 - Html.TextBox 和 Html.TextBoxFor 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5119373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com