MVC Model Preserve information on pass back to controller

A model consist of X properties.  (Say 5 string variables.)  
The strongly typed view displays 2 of the 5 properties but does not have an html element for the other 3 properties.  When the view post back the method “UpdateModel()”, works as expected, both string1 and string2 have values.  

Is there a graceful way to preserve the other 3 values?  Do I have to put them in a hidden element?  Do I have to put them in a ViewBag and copy them back one by one?

  public ActionResult Index()
     var mMode = new myModel();
    //All 5 strings filled with data.
    return View(mMode);

public ActionResult ButtonToCallNoParms()
     var myModelTest = new myModel();
     UpdateModel(myModelTest);  <<how to get the other three properties not used by view?
     return RedirectToAction("index");

@model ModelUpdateInTempForm.Models.myModel
<form action="/TestTempData/ButtonToCallNoParms" method="post">
@Html.TextBoxFor(m => m.string1);
@Html.TextBoxFor(m => m.string2);
     <input type="submit" value="Text of submit button" />

P.S. This is for a year end process and I do not care about performance, I would like to generically save all that is added to the model for the redirect.
Who is Participating?
Roopesh ReddyConnect With a Mentor IT AnalystCommented:

Then you can add few hidden variables on the page and update them back!

Also, you can store those in Session and retrieve them back!

Hope it helps u...
Roopesh ReddyIT AnalystCommented:

I think  if you set the default values of the properties, then it will retain the same after the post back, if those are not updated from the View!

Did you check this scenario?
wilfordrocksAuthor Commented:
Yes, properties not used by the view are not returned by the UpDateMode().
ambienceConnect With a Mentor Commented:
Is there a graceful way to preserve the other 3 values?  

There are a few ways.

Do I have to put them in a hidden element?  

Yes, that is one way that gives you a lot of control over whats retained and whats not. Its a graceful way, nothing wrong about it. In fact, this may be the best solution given that some of the fields are visible and others not.

Another solution, that's generic enough, is to serialize the whole model as JSON and then use client side scripting to post back as a form, but this is more suited to scenarios where you want the entire model be posted back invisibly and generically (the same piece of code works for different models), or where the models are large enough to justify the overhead.

Nonetheless, you can still combine it with some client side code that uses that JSONified model and merges user changes (UI changes) into it and posts it back.

Do I have to put them in a ViewBag and copy them back one by one?

ViewBag is for the scope of a request and is not persisted across requests. You can either use Session (server side, not graceful IMO) or have the client resend.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.