部分视图 (.ascx) 内的 Html.TextBox(“Title”) 获取值“Title”从父视图
在我的论坛项目中,我有一个部分视图 (.ascx),用于添加新的论坛帖子。论坛帖子位于主题(类别)内,这两个表都有一个名为 Title
的列。
现在的问题是,当我将部分视图放置在主题页面上时,它会自动从主题中获取标题值,从而使用主题标题
填充我的标题文本框。 不理想!
CreatePost.ascx
中的代码很简单
<label for="Title">Title</label>
<%= Html.TextBox("Title") %>
我尝试将其更改为<%= Html.TextBox ("Post.Title") %>
但 Textbox 值不会被发布。
这是正常行为吗?有没有办法可以在不使用 Javascript 清除它的情况下摆脱它?
我什至尝试使用第二个重载 Html.TextBox("Title", "some value")
设置一个值,但这只是被覆盖。
请帮忙!
In my forum project I have a partial view (.ascx) that is used for adding a new forum post. Forum posts live inside Topics (Categories) and both these tables have a column named Title
.
Now the problem is that when I place the partial view on a Topic page, it automatically grabs the Title value from the Topic, thus populating my Title Textbox with the Topic Title
. Not ideal!
The code inside the CreatePost.ascx
is simply
<label for="Title">Title</label>
<%= Html.TextBox("Title") %>
I've tried changing that to <%= Html.TextBox("Post.Title") %>
but then the Textbox value doesn't get posted.
Is this normal behaviour, and is there a way I can get rid of it without clearing it with Javascript?
I've even tried setting a value using the 2nd overload Html.TextBox("Title", "some value")
but that just gets overridden.
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 ASP.NET MVC 2,您应该始终使用强类型帮助器,它将正确处理绑定:
If you are using ASP.NET MVC 2 you should always use strongly typed helpers which will correctly handle binding:
尝试以下操作:
CreatePost.ascx
YourController.cs
之间呈现
需要确保 CreatePost.ascx在您的视图
。希望这有帮助...
Try the following:
CreatePost.ascx
YourController.cs
Need to make sure CreatePost.ascx is being rendered between
in your View.
Hope this helps...
听起来问题出在您用来渲染部分视图的代码上。 RenderPartial 将默认从父视图传递模型,因此当主题标题显示在局部视图中时您会看到这种行为。您可以覆盖它 - RenderPartial 允许您传入新模型。
类似于:
将新模型传递到部分视图(在本例中为空帖子)。
It sounds like the issue is with the code you are using to render the partial view. RenderPartial will be default pass through the model from the parent view, hence the behaviour you are seeing when the Topic title is displayed in the partial. You can override this though - one of the parameter of RenderPartial allow you to pass in a new model.
Something like:
to pass in a new model to the partial view (in this case an empty Post).