这是视图:
<%@ 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>
控制器:
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();
}
我想在这里实现的只是将所选值显示为同一视图上的文本。问题是即使我有一个选定的值,ViewBag 也会返回 null。当我测试它时,它会抛出空异常。