I have the following scenario for a web application:
A user registers a company, for this I load a company Create view.
Inside the create page, I have a dropdown list of Contact Persons for the company. Initially this list is empty, the user can click on a create button to create a new address.
This loads a partial view (as JQuery popup) for the Contact person. After the user created a new Contact person, the view returns to the Company registration view. The Company registration view must refresh with the newly added Contact Person in the dropdown box. It must also keep any data already entered.
The following is the same scenario as mine, but due to design constraints I cannot use JSON. How to pass the full Model from view to controller via jquery in a MVC c# application
MY QUESTION: Is there any way I can store the entire model in the view into Session, to use in my controller?
I tried the following:
<div class="editor-label">
@Html.LabelFor(model => model.CompanyName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CompanyName)
@Html.ValidationMessageFor(model => model.CompanyName)
@Session.Add("CompanyName",Model.CompanyName)
</div>
As this is void, it does not return a value, and cannot render.
So I tried passing the model as a parameter to the controller, but it keeps being null:
public ActionResult CreateContactPerson(ManagementAgencyViewModel viewModel)
{
if(HttpContext.Session != null)
HttpContext.Session.Add("MaObject",viewModel);
return RedirectToAction("Create", "ContactPersons");
}
Using an actionLink to call the Company Controller named "ManagementAgencies":
@Html.ActionLink(@ViewResources.Link_New_ContactPerson, "CreateContactPerson", "ManagementAgencies", null, new { @class = "openDialog ui-button ui-state-default ui-corner-all", data_dialog_id = "createDialog", data_dialog_title = @ViewResources.DialogTitle_CreateNew })
Is there any alternatives to storing in Session, WITHOUT using hidden fields or JSON? If so, what is the correct way to store model values into session from within the view?