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.
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.
Comments