Я использую ModelView для создания DropDownList:
ModelView
public class CreateSubjectModel
{
public Subject Subject { get; set; }
public IEnumerable<SelectListItem> Careers { get; set; }
}
Действие [HttpGet]
public ActionResult Create()
{
CreateSubjectModel model = new CreateSubjectModel();
List<SelectListItem> careerList = new List<SelectListItem>();
foreach (var career in _careerManager.GetCareers())
careerList.Add(new SelectListItem() { Text = career.Institution + " - " + career.Name, Value = career.id.ToString() });
model.Careers = careerList;
return View(model);
}
Действие [HttpPost]
public ActionResult Create(CreateSubjectModel createSubjectModel)
{
try
{
_subjectManager.SaveSubject(createSubjectModel.Subject);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
И просмотр:
<div class="editor-field">
@Html.DropDownListFor(model => model.Careers, Model.Careers)
@Html.ValidationMessageFor(model => model.Careers)
</div>
Проблема в том, что когда я нажимаю Отправить, у меня возникает такая проблема:
**Object reference not set to an instance of an object.**
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 47: </div>
Line 48: <div class="editor-field">
Line 49: @Html.DropDownListFor(model => model.Careers, Model.Careers)
Line 50: @Html.ValidationMessageFor(model => model.Careers)
Line 51: </div>
Я смотрю везде и не могу найтирабочий пример или какой-то учебник, который мне помогает.Надеюсь, что вы можете сделать ...