gpt4 book ai didi

c# - MVC View 没有将模型传回 Controller ,出现奇怪的错误?

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:48 24 4
gpt4 key购买 nike

这是我第二次遇到这种情况,我不知道发生了什么。我有一个 MVC View ,它是强类型的,并由 Controller 使用:

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-->

但是,每当我尝试使用“保存更改”按钮(或我放在页面上的任何其他提交按钮)提交表单时,我都会收到那些令人讨厌的 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 忘记了它已经被赋予了一个模型,因为它显然正在寻找调用 Controller 中带有签名的方法

公共(public) ActionResult EditSubscriptionSubmitAction(){}

而不是

public ActionResult EditSubscriptionSubmitAction (DutiesWeb.Models.EditSubscriptionViewModel model) {}

我不知道为什么。有人有什么想法吗?

最佳答案

您的模型必须有一个无参数的构造函数。 DefaultModelBinder 首先需要初始化模型实例(在内部使用 Activator.GetInstance()),这需要一个无参数构造函数。

在你的模型中

public class EditSubscriptionViewModel
{
// parameterless constructor
public EditSubscriptionViewModel()
{
}
public EditSubscriptionViewModel(string subName)
{
// set property values
}
....
}

关于c# - MVC View 没有将模型传回 Controller ,出现奇怪的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32228740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com