wilfordrocks
asked on
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?
Controller:
[HttpGet]
public ActionResult Index()
{
var mMode = new myModel();
//All 5 strings filled with data.
return View(mMode);
}
[HttpPost]
public ActionResult ButtonToCallNoParms()
{
var myModelTest = new myModel();
UpdateModel(myModelTest); <<how to get the other three properties not used by view?
return RedirectToAction("index");
}
View:
@model ModelUpdateInTempForm.Mode ls.myModel
<form action="/TestTempData/Butt onToCallNo Parms" method="post">
@Html.TextBoxFor(m => m.string1);
@Html.TextBoxFor(m => m.string2);
<input type="submit" value="Text of submit button" />
</form>
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.
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?
Controller:
[HttpGet]
public ActionResult Index()
{
var mMode = new myModel();
//All 5 strings filled with data.
return View(mMode);
}
[HttpPost]
public ActionResult ButtonToCallNoParms()
{
var myModelTest = new myModel();
UpdateModel(myModelTest); <<how to get the other three properties not used by view?
return RedirectToAction("index");
}
View:
@model ModelUpdateInTempForm.Mode
<form action="/TestTempData/Butt
@Html.TextBoxFor(m => m.string1);
@Html.TextBoxFor(m => m.string2);
<input type="submit" value="Text of submit button" />
</form>
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.
ASKER
Yes, properties not used by the view are not returned by the UpDateMode().
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?