简体   繁体   English

MVC视图未将模型传递回控制器,奇怪的错误?

[英]MVC View not passing back model to controller, weird error?

this is the second time this has happened to me now, and I have no idea what is going on. 这是我第二次发生这种情况,我不知道发生了什么。 I have a MVC view that is strongly typed and being fed in the model by the controller using : 我有一个MVC视图,该视图具有强类型并由控制器使用以下命令输入模型中:

public ActionResult EditSubscription (String subName){

                DutiesWeb.Models.EditSubscriptionViewModel mod = new Models.EditSubscriptionViewModel(subName);
                return View("EditSubscription", mod);

        }

everything then displays fine on the edit page: 然后,所有内容都会在编辑页面上正常显示:

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

But, whenever I try and submit the form using the 'Save Changes' button (or any other submit button I put on the page), I get one of those nasty IIS white-and-yellow errors stating : 但是,每当我尝试使用“保存更改”按钮(或我在页面上放置的任何其他提交按钮)提交表单时,我都会收到以下令人讨厌的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.

Its almost as if the View has forgotten it has been given a model, as it's obviously looking to invoke a method in the controller with the signature 几乎就像视图已经忘记了一样,已经为其提供了一个模型,因为它显然正在寻找使用签名的控制器中的方法

public ActionResult EditSubscriptionSubmitAction(){} 公共ActionResult EditSubscriptionSubmitAction(){}

rather than 而不是

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

and I have no idea why. 我不知道为什么 Anyone have any ideas? 有人有想法么?

Your model must have a parameterless constructor. 您的模型必须具有无参数的构造函数。 The DefaultModelBinder first needs to initialize and instance of your model (internally using Activator.GetInstance() ) which requires a parameterless constructor. 首先, DefaultModelBinder需要初始化和模型实例(内部使用Activator.GetInstance() ),该模型需要无参数构造函数。

In your model 在您的模型中

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

The method EditSubscriptionSubmitAction takes a parameter of type EditSubscriptionViewModel which has a constructor defined which takes a string parameter. 方法EditSubscriptionSubmitAction接受类型为EditSubscriptionViewModel的参数,该参数具有定义的构造函数,该构造函数接受string参数。

The ModelBinder cannot instantiate this object, so remove the constructor and maybe consider having a property instead. ModelBinder无法实例化此对象,因此请删除构造函数,并可能考虑使用一个属性。

public class EditSubscriptionViewModel
{
    public string SubName { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM