Here’s the view:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<TravelDeskWebsite.Models.TestModel>" %>
<!DOCTYPE html>
<html>
<head>
<title>TestPage</title>
</head>
<body>
<% using (Html.BeginForm())
{ %>
<%= Html.DropDownListFor(x => x.SelectedText, ViewBag.Tests as SelectList)%>
<%: (string)ViewBag.Test %>
<% } %>
</body>
</html>
The controller:
public ActionResult TestFunc(TestModel model)
{
List<string> TestList = new List<string>();
TestList.Add("Help");
TestList.Add("Please");
ViewBag.Tests = new SelectList(TestList, model.SelectedText);
ViewBag.Test = model.SelectedText;
return View();
}
What I wanted to achieve here is just to display the selected value as text on the same view. The problem is that the ViewBag returns null even if I have a selected value. When I test it, it throws null exception.
You will have your SelectedValue in the
SelectedTextProperty. try to use that.