我是 C# 和 MVC 的新手,所以请原谅我的幼稚/愚蠢。我已经看到多个帖子处理我遇到的错误,并且我已经尝试了所有解决方案,但它们似乎不适用于我的情况。请指导我找到解决此问题的正确方法。
背景: 我有 2 个模型 tblPrograms 和 tblReports。我正在尝试构建一个简单的表单,用户可以在其中创建一个新的 tblReport。创建新 tblReport 的部分过程是从列表中选择一个 tblProgram。
代码概述:
控制器:
// GET: /Reports/
public ViewResult New()
{
ProgramListModel progList = new ProgramListModel();
tblReports2 report = new tblReports2();
ExistingReportViewModel ervm = new ExistingReportViewModel(report,progList);
return View(ervm);
}
[HttpPost]
public ActionResult GenerateSQL(ExistingReportViewModel evrm)
{
evrm.Report.ProgramName = evrm.progList.selectedProgram;
return View(evrm);
}
查看型号:
public class ExistingReportViewModel
{
public tblReports2 Report { get; set; }
public ProgramListModel progList { get; set; }
//public ExistingReportViewModel() { }
public ExistingReportViewModel(tblReports2 report, ProgramListModel progList)
{
this.Report = report;
this.progList = progList;
}
}
楷模:
public class tblReports2
{
[Key]
public string ProgramName { get; set; }
public string ReportDesc { get; set; }
....
}
public class ProgramListModel
{
public SelectList progList;
public string selectedProgram;
public ProgramListModel() {
ReportHelper helper = new ReportHelper();
this.progList = helper.getProgramList();
}
}
辅助方法:
public SelectList getProgramList()
{
var programs = from p in this.DB.tblProgramsSet
select p;
programs = programs.Where(s => s.ProgramType.Equals("RPT"));
SelectList list = new SelectList(programs, "ProgramName", "ProgramDescription");
return list;
}
看法:
@model ReportsSetup.Models.ExistingReportViewModel
@{
ViewBag.Title = "New";
}
New
@using (Html.BeginForm("GenerateSQL", "Reports"))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>ExistingReportViewModel</legend>
@Html.DropDownListFor(model=>model.progList.selectedProgram, Model.progList.progList)
<div class="editor-label">
@Html.LabelFor(model => model.Report.ReportDesc)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Report.ReportDesc)
@Html.ValidationMessageFor(model => model.Report.ReportDesc)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Report.Formerly)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Report.Formerly)
@Html.ValidationMessageFor(model => model.Report.Formerly)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Report.ExtendedDesc)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Report.ExtendedDesc)
@Html.ValidationMessageFor(model => model.Report.ExtendedDesc)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Report.ReportCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Report.ReportCode)
@Html.ValidationMessageFor(model => model.Report.ReportCode)
</div>
...
<input type="submit" value="Create" />
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
错误:
![Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.][1]
http://i.stack.imgur.com/GHJpG.jpg
尝试的解决方案:
重构代码以在 ProgramListModel 中使用 IEnumerable <tblPrograms> 而不是 SelectList,并使用类似于以下示例的函数在 View 中转换为 IEnumerable:
public static IEnumerable<SelectListItem> ToSelectListItems( this IEnumerable<Album> albums, int selectedId) { return albums.OrderBy(album => album.Name) .Select(album => new SelectListItem { Selected = (album.ID == selectedId), Text = album.Name, Value = album.ID.ToString() }); }
重构代码以使用 IEnumerable<SelectListItem> 而不是 ProgramListModel 中的 SelectList
提前谢谢了!!