ASP MVC ViewData 从一个视图到另一个视图 (Html.Encode())

发布于 2024-09-03 03:48:16 字数 390 浏览 5 评论 0原文

我有一个页面,上面有一堆标签和复选框。在此页面上,项目部署后需要轻松自定义标签。

所以我用这种风格制作了所有标签:

Html.Encode(ViewData["lblText"])

我在页面上添加了一个名为“编辑按钮标签”的按钮,只有管理员才能看到。

单击该按钮时,我想加载另一个仅包含两列表格的视图。一列需要包含当前标签,另一列应该有文本框供用户输入新标签。

然后,一旦进行任何更改,我需要永久更改原始页面上每个标签的“lblText”。

我尝试使用 return view() 和 return RedirectToAction() 将 viewdata 和 tempdata 传递到“编辑按钮标签”视图,但没有成功。我错过了一些小事还是有更好的方法来做到这一点?

I have a page with a bunch of labels and checkboxes on it. On this page, the labels need to be easily customizable after the project is deployed.

So I made all of the labels in this style:

Html.Encode(ViewData["lblText"])

And I added a button on the page called "Edit Button Labels" that will only be seen by admins.

When that button is clicked, I would like to load another view that simply contains a table of two columns. One column needs to contain the current labels and the other should have text boxes for the user to enter new labels.

Then, once any changes have been made, I need to permanently change the "lblText" for each label on the original page.

I have tried passing viewdata and tempdata to the "Edit Button Labels" view using both return view() and return RedirectToAction() with no success. Am I missing something minor or is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不回头走下去 2024-09-10 03:48:16

我要说的第一件事是不要使用 ViewData。将包含标签的模型返回到您的视图。如果需要,创建复合模型。

然后,要编辑标签,请转到采用相同模型的视图,允许您输入新文本并将结果保存到 xml、db、文本文件等中。

所以你可能有一个这样的模型;

public class Labels
{
  public List<string> text{ get; set; }
}

因此,在数据库层中,无论在哪里,都用标签文本项填充对象。

在您的编辑视图中,您应该执行以下操作:

<% Html.PartialView("EditLabels", Model); %>

然后你有一个名为 EditLabels 的部分视图,它会具有类似于以下 psuedo 的内容;

foreach(string item in Model)
{
  <% Html.PartialView("labelEdit", item); %>
}

最后,您将获得一个局部视图,它采用单个项目并允许您对其进行编辑,称为 labelEdit。

在我看来,这是在 MVC 中执行此操作的正确方法,因为它将整个视图分解为功能块并很好地分离了关注点。

显然,您仍然需要一个 ActionResult 来发表帖子并影响更改。

Don't use ViewData is the first thing I'd say. Return a model to your view which contains the labels. Create a composite model if required.

Then, to edit your labels, go to a view that takes the same model, allows you to enter the new text and saves the results to the xml, db, text file, whatever.

So you might have a model like this;

public class Labels
{
  public List<string> text{ get; set; }
}

So in your db layer, wherever that is, fill the object with the label text items.

On your EDIT view you should then do the following imho;

<% Html.PartialView("EditLabels", Model); %>

Then you have a partial view called EditLabels and it would have something like the following psuedo;

foreach(string item in Model)
{
  <% Html.PartialView("labelEdit", item); %>
}

Then finally you have a partial view that takes a single item and allows you to edit it called labelEdit.

This, in my opinion, is the correct way to do it in MVC as to breaks the entire view into chunks of functionality and seperates the concerns nicely.

Obviously you still need an ActionResult to take a post and affect the changes.

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