Asp.net mvc 默认模型绑定抛出";没有为此对象定义无参数构造函数“;

Asp.net mvc 默认模型绑定抛出";没有为此对象定义无参数构造函数“;,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,我有一个表单,我们希望为一个客户输入多个交易。表单的viewmodel如下所示: public class TradeSpendingEntryViewModel { public TradeSpendingEntryViewModel() { Records = new List<TradeSpendingEntryViewModelRecord>(); } public string CustomerNumber { get; s

我有一个表单,我们希望为一个客户输入多个交易。表单的viewmodel如下所示:

public class TradeSpendingEntryViewModel
{
    public TradeSpendingEntryViewModel()
    {
        Records = new List<TradeSpendingEntryViewModelRecord>();
    }

    public string CustomerNumber { get; set; }
    public DateTime Date { get; set; }
    public SelectList PlanningYears { get; set; }

    public List<TradeSpendingEntryViewModelRecord> Records { get; set; }
}
当我尝试将数据发布到我的控制器时,我得到一个错误,指示“没有为此对象定义无参数构造函数:

[HttpPost]
    public ActionResult Index(TradeSpendingEntryViewModel vm)
    {
        try
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            // TODO: Add insert logic here
            return View(vm);
        }
        catch
        {
            return View();
        }
    }
堆栈跟踪指示它在模型绑定期间发生:

[MissingMethodException:没有为此对象定义无参数构造函数。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandleInternal&ctor、Boolean&bNeedSecurityCheck)+0 System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache、StackCrawlMark和stackMark)+113 System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly、布尔skipCheckThis、布尔fillCache、StackScrawlMark和stackMark)+232 System.Activator.CreateInstance(类型,布尔非公共)+83 System.Activator.CreateInstance(类型)+6 CreateModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Type modelType)+183

我的问题一定源于我是如何设置视图的,我基于本文对视图进行了标记

@使用(Html.BeginForm())
{
输入客户交易

客户详细信息 日期: 客户编号: @Html.TextBoxFor(m=>m.CustomerNumber,新的{id=“customer number”}) 客户名称: 规划年度: @Html.DropDownList(“PlanningYear”,Model.PlanningYears)
业务范围 产品代码 产品说明 津贴
类型 案例 结束当前交易 开始日期 结束日期 比率 @foreach(Model.Records中的var r) { @Html.DropDownList(“记录[0].AllowType”,r.AllowType) 对 不 评论: } }
因此,我希望第一个字段集中的字段能够映射到TradeSpendingEntryViewModel对象的直接属性(CustomerName、Date、PlanningYears)。然后,对于表示TradeSpendingEntryViewModelRecord的每个对象,都将作为TradeSpendingEntryViewModel.Records集合中的一项绑定。相反,我只得到一个神秘的“未定义无参数构造函数”异常,尽管ViewModel和record对象都具有无参数构造函数

我的问题是,我可以使用前面提到的文章中指定的约定使用默认模型绑定器,还是需要为此构建自定义模型绑定器

为完整起见,以下是用户通过javascript向表单动态添加一行后生成的表单标记:

<form method="post" action="/TradeSpendingEntry/Index">
<header class="clearfix">
<img alt="Irving Consumer Products" src="../../Content/images/logo.png">
<h1>Enter Customer Deals</h1>
</header>
<hr>
<fieldset>
<legend>Customer details</legend>
<table>
<tbody>
<tr>
<th> Date: </th>
<td>
<input id="dp1363608756704" class="hasDatepicker" type="date" value="18/03/2013" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
</tr>
<tr>
<th> Customer Number: </th>
<td>
<input id="customer-number" type="text" value="" name="CustomerNumber">
<a id="change-customer-number" href="#">Change me</a>
</td>
</tr>
<tr>
<th> Customer Name: </th>
<td>
<input id="customer-name" type="text" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
</tr>
<tr>
<th> Planning Year: </th>
<td>
<select id="PlanningYears" name="PlanningYears">
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
<a id="change-planning-year" href="#">Change me</a>
</td>
</tr>
</tbody>
</table>
</fieldset>
<div class="buttonGroup">
<input id="add-line" type="button" value="Add line">
<input id="copy-line" type="button" value="Copy line">
<input id="delete-line" type="button" value="Delete line">
</div>
<hr>
<table id="tradeSpendingEntry">
<thead>
<tr>
<th> </th>
<th> Line of business </th>
<th> Product Code </th>
<th> Product Description </th>
<th>
Allowance
<br>
Type
</th>
<th> Cases </th>
<th> End Current Deal </th>
<th> Start Date </th>
<th> End Date </th>
<th> Rate </th>
</tr>
</thead>
<tbody data-entry-index="0">
<tr>
<td>
<input type="checkbox">
</td>
<td>
<input class="lobSelect" type="text" name="records[0].LOB">
</td>
<td>
<input type="text" name="records[0].ProductCode">
</td>
<td>
<input class="product-description" type="text" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
<td>
<select id="records_0__AllowType" name="records[0].AllowType">
<option selected="selected">BillBack$</option>
<option>Billback%</option>
<option>O&A%</option>
<option>Coop%</option>
<option>VR%</option>
<option>Lump - O&A$</option>
<option>Lump - CP$</option>
<option>Lump - VR$</option>
<option>Lump - BB$</option>
</select>
</td>
<td>
<input type="number" name="records[0].Cases">
</td>
<td>
<select name="records[0].EndCurrentDeal">
<option selected="selected" value="true">Yes</option>
<option value="false">No</option>
</select>
</td>
<td>
<input id="dp1363608756707" class="hasDatepicker" type="date" name="records[0].StartDate">
</td>
<td>
<input id="dp1363608756708" class="hasDatepicker" type="date" name="records[0].EndDate">
</td>
<td>
<input type="text" name="records[0].DealRate">
</td>
</tr>
<tr>
<td></td>
<td> Comments: </td>
<td colspan="8">
<input class="comment" type="text" name="records[0].Comments">
</td>
</tr>
</tbody>
<tbody data-entry-index="1">
<tr>
<td>
<input type="checkbox">
</td>
<td>
<input class="lobSelect" type="text" name="records[1].LOB">
</td>
<td>
<input type="text" name="records[1].ProductCode">
</td>
<td>
<input class="product-description" type="text" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
<td>
<select id="records_0__AllowType" name="records[1].AllowType">
<option selected="selected">BillBack$</option>
<option>Billback%</option>
<option>O&A%</option>
<option>Coop%</option>
<option>VR%</option>
<option>Lump - O&A$</option>
<option>Lump - CP$</option>
<option>Lump - VR$</option>
<option>Lump - BB$</option>
</select>
</td>
<td>
<input type="number" name="records[1].Cases">
</td>
<td>
<select name="records[1].EndCurrentDeal">
<option selected="selected" value="true">Yes</option>
<option value="false">No</option>
</select>
</td>
<td>
<input id="dp1363608756709" class="hasDatepicker" type="date" name="records[1].StartDate">
</td>
<td>
<input id="dp1363608756710" class="hasDatepicker" type="date" name="records[1].EndDate">
</td>
<td>
<input type="text" name="records[1].DealRate">
</td>
</tr>
<tr>
<td></td>
<td> Comments: </td>
<td colspan="8">
<input class="comment" type="text" name="records[1].Comments">
</td>
</tr>
</tbody>
</table>
<footer>
<div class="buttonGroup">
<input type="submit" value="Submit Changes">
<input type="button" value="Main Menu">
<input type="button" value="View Customer Deals">
</div>
</footer>
</form>

输入客户交易

客户详细信息 日期: 客户编号: 客户名称: 规划年度: 2011 2012 2013 2014
业务范围 产品代码 产品说明 津贴
类型 案例 结束当前交易 开始日期 结束日期 比率 弹回$ 投资回收% O&A% 合作社% VR% 一次性付款-O&A$ 肿块-CP$ 肿块-VR$ 肿块-BB$ 对 不 评论: 弹回$ 投资回收% O&A% 合作社% VR% 一次性付款-O&A$ 肿块-CP$ 肿块-VR$ 肿块-BB$ 对 不 评论:
毫无疑问,这是
选择列表。
我昨天也遇到了同样的问题

如果查看SelectList,它的所有构造函数都需要一个参数

问题在于调用
公共操作结果索引(TradeSpendingEntryViewModel vm)

控制器尝试将在
POST
上发回的数据绑定到
TradeSpendingEntryViewModel
,需要在该数据上设置
PlanningYears
的值,通过创建新的
SelectList

为了解决这个问题,哟
@using (Html.BeginForm())
{
<header class="clearfix">
        <img src="../../Content/images/logo.png" alt="Irving Consumer Products" />
        <h1>Enter Customer Deals</h1>
    </header>
    <hr />
    <fieldset>
        <legend>Customer details</legend>
        <table>
            <tr>
                <th>
                    Date:
                </th>
                <td>
                    <input type="date" disabled="disabled" value="@DateTime.Today.ToShortDateString()" />
            </tr>
            <tr>
                <th>
                    Customer Number:
                </th>
                <td>
                    @Html.TextBoxFor(m => m.CustomerNumber, new { id = "customer-number" })
                    <a href="#" id="change-customer-number">Change me</a>
                </td>
            </tr>
            <tr>
                <th>
                    Customer Name:
                </th>
                <td>
                    <input type="text" id="customer-name" disabled="disabled" />
                </td>
                <tr>
                    <th>
                        Planning Year:
                    </th>
                    <td>
                        @Html.DropDownList("PlanningYear", Model.PlanningYears)
                        <a href="#" id="change-planning-year">Change me</a>
                    </td>
                </tr>
        </table>
    </fieldset>
    <div class="buttonGroup">
        <input type="button" value="Add line" id="add-line">
        <input type="button" value="Copy line" id="copy-line">
        <input type="button" value="Delete line" id="delete-line">
    </div>
    <hr />
    <table id="tradeSpendingEntry">
        <thead>
            <tr>
                <th>
                </th>
                <th>
                    Line of business
                </th>
                <th>
                    Product Code
                </th>
                <th>
                    Product Description
                </th>
                <th>
                    Allowance
                    <br />
                    Type
                </th>
                <th>
                    Cases
                </th>
                <th>
                    End Current Deal
                </th>
                <th>
                    Start Date
                </th>
                <th>
                    End Date
                </th>
                <th>
                    Rate
                </th>
            </tr>
        </thead>
        @foreach (var r in Model.Records)
        {
            <tbody data-entry-index="0">
            <tr>
                <td>
                    <input type="checkbox" />
                </td>
                <td>
                    <input type="text" name="records[0].LOB" class="lobSelect" value="@r.LOB">
                </td>
                <td>
                    <!--<input type="hidden" name="records[0]ProductCodeSelected" value="@r.ProductCode" />-->
                    <input type="text" name="records[0].ProductCode" value="@r.ProductCode">
                </td>
                <td>
                    <input type="text" class="product-description" disabled="disabled" />
                </td>
                <td>
                    @Html.DropDownList("records[0].AllowType", r.AllowType)
                </td>
                <td>
                    <input name="records[0].Cases" type="number" />
                </td>
                <td>
                    <select name="records[0].EndCurrentDeal">
                        <option value="true" selected="selected">Yes</option>
                        <option value="false">No</option>
                    </select>
                </td>
                <td>
                    <input type="date" name="records[0].StartDate" />
                </td>
                <td>
                    <input type="date" name="records[0].EndDate" />
                </td>
                <td>
                    <input type="text" name="records[0].DealRate" />
                </td>
                </tr>
            <tr>
                <td></td>
                <td>
                    Comments:
                </td>
                <td colspan="8">
                    <input type="text" class="comment" name="records[0].Comments" />
                </td>
                </tr>
        </tbody>
        }
    </table>

    <footer>
        <div class="buttonGroup">
        <input type="submit" value="Submit Changes">
        <input type="button" value="Main Menu">
        <input type="button" value="View Customer Deals">
    </div>
    </footer>
    }
<form method="post" action="/TradeSpendingEntry/Index">
<header class="clearfix">
<img alt="Irving Consumer Products" src="../../Content/images/logo.png">
<h1>Enter Customer Deals</h1>
</header>
<hr>
<fieldset>
<legend>Customer details</legend>
<table>
<tbody>
<tr>
<th> Date: </th>
<td>
<input id="dp1363608756704" class="hasDatepicker" type="date" value="18/03/2013" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
</tr>
<tr>
<th> Customer Number: </th>
<td>
<input id="customer-number" type="text" value="" name="CustomerNumber">
<a id="change-customer-number" href="#">Change me</a>
</td>
</tr>
<tr>
<th> Customer Name: </th>
<td>
<input id="customer-name" type="text" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
</tr>
<tr>
<th> Planning Year: </th>
<td>
<select id="PlanningYears" name="PlanningYears">
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
<a id="change-planning-year" href="#">Change me</a>
</td>
</tr>
</tbody>
</table>
</fieldset>
<div class="buttonGroup">
<input id="add-line" type="button" value="Add line">
<input id="copy-line" type="button" value="Copy line">
<input id="delete-line" type="button" value="Delete line">
</div>
<hr>
<table id="tradeSpendingEntry">
<thead>
<tr>
<th> </th>
<th> Line of business </th>
<th> Product Code </th>
<th> Product Description </th>
<th>
Allowance
<br>
Type
</th>
<th> Cases </th>
<th> End Current Deal </th>
<th> Start Date </th>
<th> End Date </th>
<th> Rate </th>
</tr>
</thead>
<tbody data-entry-index="0">
<tr>
<td>
<input type="checkbox">
</td>
<td>
<input class="lobSelect" type="text" name="records[0].LOB">
</td>
<td>
<input type="text" name="records[0].ProductCode">
</td>
<td>
<input class="product-description" type="text" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
<td>
<select id="records_0__AllowType" name="records[0].AllowType">
<option selected="selected">BillBack$</option>
<option>Billback%</option>
<option>O&A%</option>
<option>Coop%</option>
<option>VR%</option>
<option>Lump - O&A$</option>
<option>Lump - CP$</option>
<option>Lump - VR$</option>
<option>Lump - BB$</option>
</select>
</td>
<td>
<input type="number" name="records[0].Cases">
</td>
<td>
<select name="records[0].EndCurrentDeal">
<option selected="selected" value="true">Yes</option>
<option value="false">No</option>
</select>
</td>
<td>
<input id="dp1363608756707" class="hasDatepicker" type="date" name="records[0].StartDate">
</td>
<td>
<input id="dp1363608756708" class="hasDatepicker" type="date" name="records[0].EndDate">
</td>
<td>
<input type="text" name="records[0].DealRate">
</td>
</tr>
<tr>
<td></td>
<td> Comments: </td>
<td colspan="8">
<input class="comment" type="text" name="records[0].Comments">
</td>
</tr>
</tbody>
<tbody data-entry-index="1">
<tr>
<td>
<input type="checkbox">
</td>
<td>
<input class="lobSelect" type="text" name="records[1].LOB">
</td>
<td>
<input type="text" name="records[1].ProductCode">
</td>
<td>
<input class="product-description" type="text" disabled="disabled" style="background-color: rgb(238, 238, 238);">
</td>
<td>
<select id="records_0__AllowType" name="records[1].AllowType">
<option selected="selected">BillBack$</option>
<option>Billback%</option>
<option>O&A%</option>
<option>Coop%</option>
<option>VR%</option>
<option>Lump - O&A$</option>
<option>Lump - CP$</option>
<option>Lump - VR$</option>
<option>Lump - BB$</option>
</select>
</td>
<td>
<input type="number" name="records[1].Cases">
</td>
<td>
<select name="records[1].EndCurrentDeal">
<option selected="selected" value="true">Yes</option>
<option value="false">No</option>
</select>
</td>
<td>
<input id="dp1363608756709" class="hasDatepicker" type="date" name="records[1].StartDate">
</td>
<td>
<input id="dp1363608756710" class="hasDatepicker" type="date" name="records[1].EndDate">
</td>
<td>
<input type="text" name="records[1].DealRate">
</td>
</tr>
<tr>
<td></td>
<td> Comments: </td>
<td colspan="8">
<input class="comment" type="text" name="records[1].Comments">
</td>
</tr>
</tbody>
</table>
<footer>
<div class="buttonGroup">
<input type="submit" value="Submit Changes">
<input type="button" value="Main Menu">
<input type="button" value="View Customer Deals">
</div>
</footer>
</form>
    //select list
    private SelectList planningYears = new SelectList(new List<YourObject>());

    public SelectList PlanningYears 
    {
        get
        {
            return planningYears;
        }
        set
        {
            locations = planningYears;
        }
    }
@Html.DropDownListFor(m => m.PlanningYears , new SelectList(Model.PlanningYears ), "choose", null)
//fill it if whatever you want from db
private List<YourList> happyList = new List<YourList>(); 

//that way mvc will never try to instantiate selectList on Action execution
public SelectList PlanningYears() 
    {
        return new SelectList(happyList,"happyID","happyDesc","");
    }
public class SelectListNew : SelectList
{
    public SelectListNew() : base(string.Empty) { }
    public SelectListNew(IEnumerable items) : base(items) { }
    public SelectListNew(IEnumerable items, object selectedValue) : base(items, selectedValue) { }
    public SelectListNew(IEnumerable items, object selectedValue, IEnumerable disabledValues) : base(items, selectedValue, disabledValues) { }
    public SelectListNew(IEnumerable items, string dataValueField, string dataTextField) : base(items, dataValueField, dataTextField) { }
    public SelectListNew(IEnumerable items, string dataValueField, string dataTextField, object selectedValue) : base(items, dataValueField, dataTextField, selectedValue) { }
    public SelectListNew(IEnumerable items, string dataValueField, string dataTextField, string dataGroupField, object selectedValue) : base(items, dataValueField, dataTextField, dataGroupField, selectedValue) { }
    public SelectListNew(IEnumerable items, string dataValueField, string dataTextField, object selectedValue, IEnumerable disabledValues) : base(items, dataValueField, dataTextField, selectedValue, disabledValues) { }
    public SelectListNew(IEnumerable items, string dataValueField, string dataTextField, string dataGroupField, object selectedValue, IEnumerable disabledValues) : base(items, dataValueField, dataTextField, dataGroupField, selectedValue, disabledValues) { }
    public SelectListNew(IEnumerable items, string dataValueField, string dataTextField, string dataGroupField, object selectedValue, IEnumerable disabledValues, IEnumerable disabledGroups) : base(items, dataValueField, dataTextField, dataGroupField, selectedValue, disabledValues, disabledGroups) { }
}