将 ViewModel 传递给控制器​​时应用程序崩溃

问题描述

  1. 当我将视图模型传递给方法时,它会崩溃,但它可以与 FormCollection 一起使用,或者没有将任何内容传递给 onpost 方法
[HttpPost]
[AutovalidateAntiforgeryToken]
public ActionResult Update(Customerinformation model)
{
    if (ModelState.IsValid)
    {
        //save
        var UpdateRecord = customerServices.Update(model);
        if (UpdateRecord)
        {
            return RedirectToAction("Details");
        }
    }
}
@model Customer.Models.CustomerinformationDetails
@using Customer.Models.Customerinformation

@{
    var customerName = Model.Name;
    ViewData["Title"] = customerName;
}

<h1>Edit information for @customerName</h1>
<hr />
@using (Html.BeginForm("Update","Customer",FormMethod.Post))
{
    <div class="form-group">
    @Html.Label("Introduction","Introduction:",htmlAttributes: new { @class = "control-label col-md-10" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Introduction,new { htmlAttributes = new { @class = "form-control" } })
    </div>
    </div>
    <div class="form-group">
    @Html.Label("Contact Person","Contact Person:",htmlAttributes: new { @class = "control-label col-md-10" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.ContactPerson,new { htmlAttributes = new { @class = "form-control" } })
    </div>
    </div>
    <div class="form-group">
    <input type="submit" value="Update" class="btn btn-primary"></input>
    </div>
}   

解决方法

添加

return View(model)

在 if 语句之外。

目前如果失败了就没有什么可返回的了。