Reputation: 82297
I was hoping this would work :( When debugged the hidden input seems to point to an object but in the post nothing shows up. Here is what I tried (note that I am trying for brevity so this is an example)
Model
public class myViewModel
{
public MyObject MyObject { get; set; }
public int MyNumber { get; set; }
}
Controller
public ActionResult displaySimpleView()
{
var mVM = new myViewModel();
mVM.MyObject = //let MyObject be filled with 10 fields of data
return View(mVM);
}
View
@model namespace.myViewModel
//display the fields of data
@using (Ajax.BeginForm("Complete", ajaxOpts))//simple Ajax Options not really relevant
{
@Html.ValidationSummary(true)
@Html.HiddenFor(m => m.MyObject)
@Html.EditorFor(m => m.MyNumber)
<p><input type="submit" value="Go" /></p>
}
Controller Again
[HttpPost]
public ActionResult getMyObject(myViewModel mVM)
{
mVM.MyObject is null here.
mVM.MyNumber has a value.
return RedirectToAction("someGetAction");
}
How can I pass MyObject to getMyObject? I would prefer to not have to have a hidden field for each property and then remap because some of those properties are nested objects.
Upvotes: 2
Views: 259
Reputation: 82297
View:
@{
TempData["passMyObject"] = Model.MyObject;
}
Controller Post:
var myObject = TempData["passMyObject"];
Upvotes: 2