开发者

How do i submit the complete viewmodel to an other view?

开发者 https://www.devze.com 2023-03-23 22:23 出处:网络
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 langua

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 t开发者_开发知识库ranslations, 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号