1

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?

Community
  • 1
  • 1
nico van vuuren
  • 105
  • 1
  • 8

1 Answers1

1

Remember, the server tags execute when the view is rendered. When the code:

@Session.Add("CompanyName",Model.CompanyName)

executes, there is no value for Model.CompanyName because the rendered view is being sent to the user's web browser. Having the code in that location literally sets it to whatever the default value for CompanyName is (probably a null at that point).

Later, the user submits the form to your action, the viewModel variable you defined in CreateContactPerson will have the user's value populated. At the point, you can save the value to the session. Just mode the session-saving code into your action and you should be good.

  • The server tags does make sence, should've known that. But at what point in time is the viewModel declared at CreateContactPerson populated? Because when the Action link directs to the CreateContactPerson method, the viewModel is still null at that point in time. I guess my question is, is the viewModel passed only ever populated from the client's side if a "submit" was used? – nico van vuuren Dec 05 '12 at 12:26