我是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
尝试解决方案:
重构代码以使用IEnumerable&lt; tblPrograms&gt;代替 ProgramListModel中的SelectList并转换为 IEnumerable在View中使用类似的函数 示例如下:
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&lt; SelectListItem&gt;代替 ProgramListModel中的SelectList
非常感谢提前!!
答案 0 :(得分:8)
不,你不能这样做:
public ActionResult GenerateSQL(ExistingReportViewModel evrm)
如果您的ExistingReportViewModel
没有无参数构造函数。如果默认模型绑定器没有默认构造函数,则它不知道如何实例化该类。
所以你有两种可能性:
为此类定义一个无参数构造函数:
public class ExistingReportViewModel
{
public ExistingReportViewModel()
{ }
... some other stuff of course
}
编写自定义模型绑定器
绝对要去1.除非你需要非常特别的东西。如果您需要非常特别的东西,请准确定义,以便我们为您提供帮助。