简体   繁体   中英

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 :

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 :

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(){}

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.

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.

The ModelBinder cannot instantiate this object, so remove the constructor and maybe consider having a property instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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