Set @Html.Dropdownlistfor to session in MVC 4

PavanKumar GVVS

After lot of tries and research , finally ended up with this.

I have couple of view(namely Index and Create) in my MVC 4 application and both having a dropdown with similar functionality. I can select a value from the Dropdown in Index and move forward to create.Also I can select the value from my Dropdown in Create view as well.

If I select any value from the index view then that value is set to a session.My problem is that if I select any value from Index view that value should be carried to the create view for which I tried with session concept.

In both of my action methods(Index and Create(Get)) I am able to bind the list of values to the dropdown which works fine.

Index view:

@Html.LabelFor(model => model.document.TeamId, "Team")  
@Html.DropDownListFor(model => model.document.TeamId, new SelectList(Model.ddlTeam, "Value", "Text"), "Select Team", new { id = "TeamID", onchange = "GetProjects()", @class = "form-control", UpdateTargetId = "atag" })
@Html.ValidationMessageFor(model => model.document.TeamId)

Now the selected value is set to a session which is working fine.(Session["TeamId"] = somevalue;)

Then in my create view , I am checking it as(which I found from somebody's post in order to assign the session value to the dropdownlistfor ) as:

Create View:

@Html.LabelFor(model => model.document.TeamId, "Team", new { @class = "control-label col-sm-3" }) 
@if (Session["TeamId"] == null)
{ 
   @Html.DropDownListFor(model => model.document.TeamId, new SelectList(Model.ddlTeam, "Value", "Text"), "Select Team", new { id = "TeamID", onchange = "GetProjects()", @class = "form-control", UpdateTargetId = "atag" })
}
else
{ 
  @Html.DropDownListFor(model => model.document.TeamId, new SelectList(Model.ddlTeam, "Value", "Text", Session["TeamId"]), "Select Team", new { id = "TeamID", onchange = "GetProjects()", @class = "form-control", UpdateTargetId = "atag" })
 }
 @Html.ValidationMessageFor(model => model.document.TeamId)

But again even after selecting the value from index view the value is not getting carried to the create view.

Please suggest where am I doing wrong or how should I make it work? Thanks in advance.

Saroj

Came across with similar issue n I solved my problem by: First populated the dropdown in GET method of the Create actionmethod(In your case it is ddlTeam and send it to the view).

Again use your session value to assign it to the property of the model in your Get action method(In your case TeamId)before sending it to the view as:

[HttpGet]
public ActionResult Create(int ID)
{
    MyModel model = new MyModel();
    model.document.TeamId = Convert.ToInt32(Session["TeamId"]);
    return view(model);
}

Now once your view is called there you need to pass this property along with the populated dropdown values.In your case it would be something like:

Create view

 @if (Session["TeamId"] == null)
{ 
    @Html.DropDownListFor(model => model.TeamId, new SelectList(Model.ddlTeam, "Value", "Text"), "Select Team", new { id = "TeamID", onchange = "GetProjects()", @class = "form-control", UpdateTargetId = "atag" })
}
else
{ 
    @Html.DropDownListFor(model => model.TeamId, new SelectList(Model.ddlTeam, "Value","Text", Model.TeamId), new { id = "TeamID", onchange = "GetProjects()", @class = "form-control" })
}

Try this , it should solve your problem since it worked for me.Please do let me know in case of issues.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

From Dev

Trouble with MVC 4 DropDownListFor

From Dev

MVC4 DropDownListFor Object reference not set to an instance of an object

From Dev

How to set Default value in MVC 4 razor DropDownListFor

From Dev

session bind with @Html.DropDownListFor

From Dev

DropDownListFor ASP MVC4

From Dev

MVC5 Razor html.dropdownlistfor set selected when value is in array

From Dev

MVC5 Razor html.dropdownlistfor set selected when value is in array

From Dev

MVC - DropDownListFor Enum - Set Default Value

From Dev

Selected value for dropdownlistfor in mvc4

From Java

Html.DropdownListFor selected value not being set

From Dev

@Html.DropDownListFor how to set default value

From Dev

Html.DropDownListFor set selected value

From Dev

How to set the login session in ASP.NET MVC 4?

From Dev

MVC DropdownlistFor set selected value not working?c#

From Dev

How to populate an dropdownlistfor from database in ASP.NET MVC 4

From Dev

MVC 4 Changing multiple display fields based on DropDownListFor selection

From Dev

ASP.NET mvc4 DropDownListFor pass back an object

From Dev

Set HTML attributes on ASP.Net MVC 4 HTML Helpers?

From Dev

Should I bind html.dropdownlistfor in POST too in MVC 5?

From Dev

ASP.NET MVC @Html.DropdownListFor wrong selected value

From Dev

MVC @Html.DropDownListFor change displayed value in javascript

From Dev

How to set default selected value for Html.DropDownListFor

From Dev

Clear session on Logout MVC 4

From Dev

End a session remotely MVC 4

From Dev

How to extend the DropDownListFor() in MVC

From Dev

MVC Entity Framework DropDownListFor<>

From Dev

How to extend the DropDownListFor() in MVC

From Dev

MVC Entity Framework DropDownListFor<>

Related Related

  1. 1

    Set @Html.Dropdownlistfor to session in MVC 4

  2. 2

    Trouble with MVC 4 DropDownListFor

  3. 3

    MVC4 DropDownListFor Object reference not set to an instance of an object

  4. 4

    How to set Default value in MVC 4 razor DropDownListFor

  5. 5

    session bind with @Html.DropDownListFor

  6. 6

    DropDownListFor ASP MVC4

  7. 7

    MVC5 Razor html.dropdownlistfor set selected when value is in array

  8. 8

    MVC5 Razor html.dropdownlistfor set selected when value is in array

  9. 9

    MVC - DropDownListFor Enum - Set Default Value

  10. 10

    Selected value for dropdownlistfor in mvc4

  11. 11

    Html.DropdownListFor selected value not being set

  12. 12

    @Html.DropDownListFor how to set default value

  13. 13

    Html.DropDownListFor set selected value

  14. 14

    How to set the login session in ASP.NET MVC 4?

  15. 15

    MVC DropdownlistFor set selected value not working?c#

  16. 16

    How to populate an dropdownlistfor from database in ASP.NET MVC 4

  17. 17

    MVC 4 Changing multiple display fields based on DropDownListFor selection

  18. 18

    ASP.NET mvc4 DropDownListFor pass back an object

  19. 19

    Set HTML attributes on ASP.Net MVC 4 HTML Helpers?

  20. 20

    Should I bind html.dropdownlistfor in POST too in MVC 5?

  21. 21

    ASP.NET MVC @Html.DropdownListFor wrong selected value

  22. 22

    MVC @Html.DropDownListFor change displayed value in javascript

  23. 23

    How to set default selected value for Html.DropDownListFor

  24. 24

    Clear session on Logout MVC 4

  25. 25

    End a session remotely MVC 4

  26. 26

    How to extend the DropDownListFor() in MVC

  27. 27

    MVC Entity Framework DropDownListFor<>

  28. 28

    How to extend the DropDownListFor() in MVC

  29. 29

    MVC Entity Framework DropDownListFor<>

HotTag

Archive