如何将完整的视图模型提交到其他视图?

发布于 2024-11-26 19:47:01 字数 1463 浏览 0 评论 0原文

我正在使用 MVC3(剃须刀),并且正在尝试执行以下操作。

我有一个片段列表。这些片段有一些常规设置,然后有未知数量的语言的翻译。

现在我正在尝试执行以下操作:

在“创建”页面上(网址:屏幕)我设置了一个片段的常规设置。下面有一个已填充翻译的列表(一开始是空的)。当您按下“Opslaan”按钮时,我希望表单保存常规设置和翻译列表。

当我按下“添加”按钮时,我想将完整的视图模型(设置+翻译列表)提交到另一个页面,我可以在其中填写翻译。填写翻译后,我想返回此页面(网址:屏幕)。此处,列表中填写了翻译。

现在我做错了一些事情,因为我无法将视图模型提交到第二页。

这是我的代码:

按钮“添加翻译”:

@Html.ActionLink("Add", "CreateTranslation", new { oSnippeteditviewmodel = this.Model }, null)

SnippetController:

public ActionResult Create()
{
    SnippetEditViewModel oItem = new SnippetEditViewModel();
    oItem.lSnippetsPerLanguage = new List<SnippetPerLanguageEditViewModel>();
    return View(oItem);
} 

[HttpPost]
public ActionResult Create(SnippetEditViewModel Snippeteditviewmodel)
{
    if (ModelState.IsValid)
    {
        Snippeteditviewmodel.Bookmark = Snippeteditviewmodel.Bookmark.Replace(' ', '_');
        _repoSnippet.CreateSnippet(Snippeteditviewmodel);
        return RedirectToAction("Index");  
    }

    return View(Snippeteditviewmodel);
}

public ActionResult CreateTranslation(SnippetEditViewModel oSnippeteditviewmodel)
{
    return View(oSnippeteditviewmodel);
} 

在控制器中,操作 CreateTranslation 对象“oSnippeteditviewmodel”保持为空。

有谁有类似的问题吗?或者解决方案?

I'm using MVC3 (razor) and i'm trying to get the following working.

I have a list of snippets. These snippets have some general settings and then have a translation for an unknown ammount of languages.

Now i'm trying to do the following:

On the 'Create' page (url: Screen) of a snippet i set the general settings. under that there is a list of filled translations (empty at the start). When you press the 'Opslaan' button, i want the form to save the general settings and the list of translations.

When i push the 'Add' button i want to submit the complete viewmodel (settings + list of translations) to an other page where i can fill in a translation. After i filled in a translations, i want to return to this page (url: Screen). Here, a translation is filled in the list.

Now i'm doing someting wrong, because i cant get the viewmodel to submit to the 2nd page.

this is my code:

button 'add translation':

@Html.ActionLink("Add", "CreateTranslation", new { oSnippeteditviewmodel = this.Model }, null)

SnippetController:

public ActionResult Create()
{
    SnippetEditViewModel oItem = new SnippetEditViewModel();
    oItem.lSnippetsPerLanguage = new List<SnippetPerLanguageEditViewModel>();
    return View(oItem);
} 

[HttpPost]
public ActionResult Create(SnippetEditViewModel Snippeteditviewmodel)
{
    if (ModelState.IsValid)
    {
        Snippeteditviewmodel.Bookmark = Snippeteditviewmodel.Bookmark.Replace(' ', '_');
        _repoSnippet.CreateSnippet(Snippeteditviewmodel);
        return RedirectToAction("Index");  
    }

    return View(Snippeteditviewmodel);
}

public ActionResult CreateTranslation(SnippetEditViewModel oSnippeteditviewmodel)
{
    return View(oSnippeteditviewmodel);
} 

And in the controller, action CreateTranslation the object 'oSnippeteditviewmodel' stays null.

annyone who has a simular problem? Or a solution?

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

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

发布评论

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

评论(2

葬心 2024-12-03 19:47:01

首先,您应该尝试生成这样的操作链接。

@Html.ActionLink("Add", "CreateTranslation", this.Model, null)

在这种情况下,mvc 将尝试为您的链接传递正确的序列化模型值,如果您的模型足够简单,CreateTranslations 将正确获取其模型。但是,我不会那样做。生成的链接是静态的。如果用户在客户端更改代码片段值怎么办?当涉及到添加翻译时,所有更改的表单值都将丢失(链接将传递初始的服务器生成的值)。因此,您应该尝试以下操作之一:

  1. 创建具有两个按钮的表单,一个用于 CatingTranslation,一个用于保存。创建翻译时,动态更改表单的操作和方法参数以获取 CreateTranslation 操作。这样,表单将序列化其所有当前的片段设置并传递给所需的操作,并且您将获得传递给 CreateTranslation 操作的当前片段模型。
  2. 使用阿贾克斯。只需将翻译创建输入字段动态注入同一页面即可。这很简单,也更用户友好(没有导航捆绑),并且保留了更多的 http 流量(将所有翻译和片段传递到第二页,然后返回所有这些 + 1 翻译可能会给您带来麻烦)。我会推荐这种方法。这比第一个或您的方法要简单得多。

First, you should try to generate action link like this

@Html.ActionLink("Add", "CreateTranslation", this.Model, null)

In this case mvc will try to pass correctly serialized model values for your link and if your model is simple enough, CreateTranslations will get its model correctly. But, I would not do it that way. Generated link is static. What if user changes Snippet values on client side? When it comes to adding Translation, all the changed form values will be lost (Link will pass initial, server generated values). So, you should try one of the followings

  1. Create the form with two buttons, one for CratingTranslation and one for Saving. When creating translation, dynamically change form's action and method parameters to GET the CreateTranslation action. This way, form will serialize all its current Snippet settings and pass to desired action, and you get the current snippet model passed to CreateTranslation action.
  2. Use ajax. Just dynamically inject translation creation input fields into same page. That's simple and more user friendly (no bundle of navigations), and more http traffic is reserved (Passing all the translations and snippet to second page, and then returning all of these + 1 translation could get you in trouble). I would reccomend this approach. This is far more simple than first or your approaches.
晌融 2024-12-03 19:47:01

我没有正确地告诉你,但如果你想通过“创建”控制器添加数据,那么你不需要在“oSnippeteditviewmodel”中指定对象。 获取所有表单数据

您可以通过Request.Form["controlName"]

,并通过上面填充 Snippeteditviewmodel 数据成员并保存。

I am not getting you properly but if you wanna add data by "create" controller then you don't need to specify object in "oSnippeteditviewmodel". You can get all form data by

Request.Form["controlName"]

and fill the Snippeteditviewmodel data member by above and save that.

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