这是我现在第二次发生这种情况,我不知道发生了什么。我有一个强类型的MVC视图,并由控制器使用以下方式在模型中输入:
public ActionResult EditSubscription (String subName){
DutiesWeb.Models.EditSubscriptionViewModel mod = new Models.EditSubscriptionViewModel(subName);
return View("EditSubscription", mod);
}
然后在编辑页面上显示一切正常:
@model DutiesWeb.Models.EditSubscriptionViewModel
@{
ViewBag.Title = "EditSubscription";
}
<h2>EditSubscription</h2>
@using (Html.BeginForm("EditSubscriptionSubmitAction","Home", FormMethod.Post )) {
<div class="form-horizontal">
<div class="form-group">
<div class="col-lg-12">
</div>
</div>
<div class="form-group">
<div class="col-lg-2">
@Html.LabelFor(model => model.subscription.subscriptionName, new { @class = "control-label" })
</div>
<div class="col-lg-10">
@Html.TextBoxFor(model => model.subscription.subscriptionName, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-lg-2">
@Html.LabelFor(model => model.subscription.messageCount, new { @class = "control-label" })
</div>
<div class="col-lg-10">
@Html.TextBoxFor(model => model.subscription.messageCount, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-lg-2">
@Html.LabelFor(model => model.subscription.lastAccessed, new { @class = "control-label" })
</div>
<div class="col-lg-10">
@Html.TextBoxFor(model => model.subscription.lastAccessed, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-2">
@Html.LabelFor(model => model.subscription.status, new { @class = "control-label" })
</div>
<div class="col-md-10">
@Html.DropDownListFor(model => model.subscription.status,
(IEnumerable<SelectListItem>)Model.subscriptionActiveOptions,
"Please Select ...", new { @class = "form-control", @id = "ddlSubscriptionActive" })
@Html.ValidationMessageFor(model => model.subscription.status)
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<input type="submit" value="Save Changes" class="btn btn-primary" />
<input type="button" value="Delete" class="btn btn-danger" />
</div>
</div>
</div>} <!--Yes, I have remembered the closing bracket-->
但是,每当我尝试使用&#39;保存更改&#39;提交表单时按钮(或我放在页面上的任何其他提交按钮),我得到一个令人讨厌的IIS白色和黄色错误说明:
No parameterless constructor defined for this 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.MissingMethodException: No parameterless constructor defined for this object.
它几乎就像View已经忘记它已被赋予模型一样,因为它显然希望在控制器中使用签名调用方法
public ActionResult EditSubscriptionSubmitAction(){}
而不是
public ActionResult EditSubscriptionSubmitAction (DutiesWeb.Models.EditSubscriptionViewModel model) {}
我不明白为什么。有人有什么想法吗?
答案 0 :(得分:3)
您的模型必须具有无参数构造函数。 DefaultModelBinder
首先需要初始化模型实例(内部使用Activator.GetInstance()
),这需要无参数构造函数。
在你的模特中
public class EditSubscriptionViewModel
{
// parameterless constructor
public EditSubscriptionViewModel()
{
}
public EditSubscriptionViewModel(string subName)
{
// set property values
}
....
}
答案 1 :(得分:1)
方法EditSubscriptionSubmitAction
采用EditSubscriptionViewModel
类型的参数,该参数定义了一个带有string
参数的构造函数。
ModelBinder无法实例化此对象,因此请删除构造函数,并考虑改为使用属性。
public class EditSubscriptionViewModel
{
public string SubName { get; set; }
}