部分视图 (.ascx) 内的 Html.TextBox(“Title”) 获取值“Title”从父视图

发布于 2024-09-30 08:19:59 字数 615 浏览 0 评论 0原文

在我的论坛项目中,我有一个部分视图 (.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吖咩 2024-10-07 08:19:59

如果您使用 ASP.NET MVC 2,您应该始终使用强类型帮助器,它将正确处理绑定:

<%= Html.TextBoxFor(x => x.Category.Title) %>

If you are using ASP.NET MVC 2 you should always use strongly typed helpers which will correctly handle binding:

<%= Html.TextBoxFor(x => x.Category.Title) %>
咆哮 2024-10-07 08:19:59

尝试以下操作:

CreatePost.ascx

   <label for="Title">Title</label>
   <%= Html.TextBox("Title") %>

YourController.cs

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreatePost(string Title) {
    // do something with Title
    return View();
}

之间呈现

<% using (Html.BeginForm()) { %>....
  ..
  <% Html.RenderPartial("CreatePost.ascx");
  ..
  ..
<% } >

需要确保 CreatePost.ascx在您的视图

。希望这有帮助...

Try the following:

CreatePost.ascx

   <label for="Title">Title</label>
   <%= Html.TextBox("Title") %>

YourController.cs

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreatePost(string Title) {
    // do something with Title
    return View();
}

Need to make sure CreatePost.ascx is being rendered between

<% using (Html.BeginForm()) { %>....
  ..
  <% Html.RenderPartial("CreatePost.ascx");
  ..
  ..
<% } >

in your View.

Hope this helps...

流绪微梦 2024-10-07 08:19:59

听起来问题出在您用来渲染部分视图的代码上。 RenderPartial 将默认从父视图传递模型,因此当主题标题显示在局部视图中时您会看到这种行为。您可以覆盖它 - RenderPartial 允许您传入新模型。

类似于:

<% Html.RenderPartial("CreatePost.ascx", new Post());%>

将新模型传递到部分视图(在本例中为空帖子)。

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:

<% Html.RenderPartial("CreatePost.ascx", new Post());%>

to pass in a new model to the partial view (in this case an empty Post).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文