Please refer below sample.
Model
public class DataModels
{
public List<DataModels> Datas { get; set; }
public string Class { get; set; }
public string Stream { get; set; }
public int S_Value { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
DataModels model = new DataModels();
model.Datas = PopulateData();
return View(model);
}
[HttpPost]
public ActionResult Index(DataModels data)
{
List<DataModels> datas = PopulateData();
// Condition to be checked with database record.
if (datas.Where(x => x.Class == data.Class && x.Stream == data.Stream).Count() > 0)
{
ViewBag.Message = "Stream " + data.Stream + " is not allowed to " + data.Class + " Class";
}
data.Datas = datas;
return View(data);
}
private static List<DataModels> PopulateData()
{
List<DataModels> datas = new List<DataModels>();
datas.Add(new DataModels { Class = "P1", Stream = "H", S_Value = 48 });
datas.Add(new DataModels { Class = "P1", Stream = "G", S_Value = 48 });
datas.Add(new DataModels { Class = "P1", Stream = "F", S_Value = 48 });
datas.Add(new DataModels { Class = "P1", Stream = "E", S_Value = 48 });
datas.Add(new DataModels { Class = "P1", Stream = "D", S_Value = 48 });
datas.Add(new DataModels { Class = "P1", Stream = "C", S_Value = 48 });
datas.Add(new DataModels { Class = "P1", Stream = "B", S_Value = 48 });
datas.Add(new DataModels { Class = "P1", Stream = "A", S_Value = 48 });
return datas;
}
}
View
@model dropdownlist.Models.DataModels
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
<tr>
<td>
Class:
</td>
<td>
@Html.DropDownListFor(m => m.Class, new SelectList(Model.Datas, "Class", "Class"), "Please select")
</td>
</tr>
<tr>
<td>
Stream:
</td>
<td>
@Html.DropDownListFor(m => m.Stream, new SelectList(Model.Datas, "Stream", "Stream"), "Please select")
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert('@ViewBag.Message');
}
</script>
}
</body>
</html>
Screenshot