如何将完整的视图模型提交到其他视图?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该尝试生成这样的操作链接。
在这种情况下,mvc 将尝试为您的链接传递正确的序列化模型值,如果您的模型足够简单,CreateTranslations 将正确获取其模型。但是,我不会那样做。生成的链接是静态的。如果用户在客户端更改代码片段值怎么办?当涉及到添加翻译时,所有更改的表单值都将丢失(链接将传递初始的服务器生成的值)。因此,您应该尝试以下操作之一:
First, you should try to generate action link like this
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
我没有正确地告诉你,但如果你想通过“创建”控制器添加数据,那么你不需要在“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.