简体   繁体   English

错误“未为此对象定义无参数的构造函数。” ViewModel中的SelectList错误

[英]Error “No parameterless constructor defined for this object.” with SelectList from ViewModel

In my viewmodel I made two SelectList properties with names of cities coming from an external database. 在我的视图模型中,我创建了两个SelectList属性,这些城市的名称来自外部数据库。

ViewModel 视图模型

namespace Treinreizen.Models.ViewModels
{
    public class ReizenViewModel
    {
        public SelectList VanStad { get; set; }

        public SelectList NaarStad { get; set; }
    }
}

In my controller I use one GET method "Registratie" where the user can select cities and one POST method to post this cities. 在我的控制器中,我使用一种GET方法“ Registratie”,用户可以在其中选择城市,并使用一种POST方法来发布该城市。 The problem is that I get the error 问题是我得到了错误

"No parameterless constructor defined for this object." “没有为此对象定义无参数构造函数。”

when I try to post my form. 当我尝试发布表格时。 In the Stack Trace it says: 在堆栈跟踪中说:

"[MissingMethodException: No parameterless constructor defined for this object.]" and "[MissingMethodException: No parameterless constructor defined for this object. Object type 'System.Web.Mvc.SelectList'.]" “ [[MissingMethodException:没有为此对象定义无参数构造函数。]”和“ [[MissingMethodException:没有为此对象定义无参数构造函数。对象类型'System.Web.Mvc.SelectList'。]”

Controller 调节器

        // GET: Hotels/Registratie
        public ActionResult Registratie()
        {
            stedenServices = new StedenServices();
            ReizenViewModel registratieviewmodel = new ReizenViewModel();
            registratieviewmodel.VanStad = new SelectList(stedenServices.All(), "StadID", "Stad");
            registratieviewmodel.NaarStad = new SelectList(stedenServices.All(), "StadID", "Stad");
            return View(registratieviewmodel);
        }

        // POST: Hotels/Registratie
        [HttpPost]
        public ActionResult Registratie(ReizenViewModel registratie)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View(registratie);
        }

View 视图

    <div class="form-group">
        @Html.LabelFor(model => model.VanStad, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.VanStad, Model.VanStad, "--Selecteer Stad--")
            @Html.ValidationMessageFor(model => model.VanStad, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.NaarStad, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.NaarStad, Model.NaarStad, "--Selecteer Stad--")
            @Html.ValidationMessageFor(model => model.NaarStad, "", new { @class = "text-danger" })
        </div>
    </div>

只需向ReizenViewModel添加一个无参数的构造函数即可:

public ReizenViewModel(){}

暂无
暂无

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

相关问题 获取“未为此对象定义无参数的构造函数。”发布视图模型时出错 - Getting “No parameterless constructor defined for this object.” Error while posting a viewmodel CBO.FillCollection抛出“没有为此对象定义的无参数构造函数。”错误 - CBO.FillCollection throwing “No parameterless constructor defined for this object.” error Unity:没有为此对象定义无参数构造函数。 - Unity : No parameterless constructor defined for this object. 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 错误:未为此对象定义无参数构造函数 - Error: No parameterless constructor defined for this object 没有为此对象定义无参数构造函数。 当我尝试使用 Unity 依赖时 - No parameterless constructor defined for this object. while i try for Unity Dependency System.MissingMethodException:没有为此对象定义的无参数构造函数。 MVC4 - System.MissingMethodException: No parameterless constructor defined for this object. MVC4 “没有为此对象定义无参数的构造函数。”对于使用结构映射的控制器中的IBus - “No parameterless constructor defined for this object.” for IBus in controller using StructureMap 从数据库更新实体后发生错误:未为此对象定义无参数构造函数 - Error after updating Entity from Database: No parameterless constructor defined for this object 错误消息“未为此对象定义无参数构造函数” - Error message “No parameterless constructor defined for this object”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM