C# mvc中没有为此对象定义无参数构造函数
当我在视图中使用SelectList时,我得到错误“没有为这个对象定义无参数构造函数”,我看到了很多解决方案,但找不到适合我的解决方案。这是查看代码:C# mvc中没有为此对象定义无参数构造函数,c#,asp.net-mvc-4,razor,C#,Asp.net Mvc 4,Razor,当我在视图中使用SelectList时,我得到错误“没有为这个对象定义无参数构造函数”,我看到了很多解决方案,但找不到适合我的解决方案。这是查看代码: @using (Html.BeginForm("Product", "Products", FormMethod.Post, new { @class = "form-horizontal" })) { @Html.ValidationSummary() <div class="form-group"> &
@using (Html.BeginForm("Product", "Products", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.ValidationSummary()
<div class="form-group">
<label class="control-label col-md-3" for="SelectProduct">Select Product</label>
<div class="col-md-4">
@Html.DropDownListFor(p => p.SelectProduct, new SelectList(new[]
{
new { Value = "New", Text = "New Entry" },
new { Value = "Existing", Text = "Existing Entry" },
}, "Value", "Text"),
new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3" for="productName">Product Name</label>
<div class="col-md-8">
@Html.TextBoxFor(u => u.ProductName, new { @class = "form-control", @id = "productName" })
</div>
</div>
}
型号:
public class ProductDto
{
public ProductDto()
{
}
/// <summary>
/// Gets or sets whether its New or existing product
/// </summary>
public SelectList SelectProduct { get; set; }
/// <summary>
/// Gets or sets product name.
/// </summary>
public string ProductName { get; set; }
}
公共类产品到
{
公共产品
{
}
///
///获取或设置其新产品还是现有产品
///
公共选择列表选择产品{get;set;}
///
///获取或设置产品名称。
///
公共字符串ProductName{get;set;}
}
堆栈跟踪:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +11
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +183
System.Web.Mvc.DefaultModelBinder.BindSimpleModel(ControllerContext controllerContext, ModelBindingContext bindingContext, ValueProviderResult valueProviderResult) +329
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +368
System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +17
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +384
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +88
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +53
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1314
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +416
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +446
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +381
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +17
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
[MissingMethodException:没有为此对象定义无参数构造函数。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandleInternal&ctor、Boolean&bNeedSecurityCheck)+0
System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache、StackScrawlMark和stackMark)+119
System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly、布尔skipCheckThis、布尔fillCache、StackScrawlMark和stackMark)+232
System.Activator.CreateInstance(类型,布尔非公共)+83
System.Activator.CreateInstance(类型)+11
CreateModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Type modelType)+183
System.Web.Mvc.DefaultModelBinder.BindSimpleModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,ValueProviderResult ValueProviderResult)+329
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)+368
System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext ControllerContext、ModelBindingContext bindingContext、PropertyDescriptor PropertyDescriptor、IModelBinder propertyBinder)+17
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext ControllerContext,ModelBindingContext bindingContext,PropertyDescriptor PropertyDescriptor)+384
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext ControllerContext,ModelBindingContext bindingContext)+88
System.Web.Mvc.DefaultModelBinder.BindCompleteXelementalModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Object model)+53
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)+1314
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)+416
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext ControllerContext,ParameterDescriptor ParameterDescriptor)+317
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor)+117
System.Web.Mvc.Async.c__显示类25.b__1e(AsyncCallback AsyncCallback,Object asyncState)+446
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+130
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext ControllerContext,String actionName,AsyncCallback回调,对象状态)+302
System.Web.Mvc.c_uuudisplayClassId.b_uuu17(异步回调,异步回调,对象异步状态)+30
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+130
System.Web.Mvc.Controller.BeginExecuteCore(异步回调,对象状态)+381
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+130
System.Web.Mvc.Controller.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+317
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext RequestContext,AsyncCallback回调,对象状态)+17
System.Web.Mvc.c_uuudisplayClass8.b_uuu2(异步回调,异步回调,对象异步状态)+71
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(异步回调,对象状态,Int32超时)+130
谢谢
DropDownListFor
用于在您的模型中设置属性值-您的模型包含一个SelectProduct
属性,该属性的类型为SelectList
-我猜它应该是string
类型,因为这是SelectList
属性的类型视野中的建筑
public class ProductDto
{
public ProductDto()
{
}
/// <summary>
/// Gets or sets whether its New or existing product
/// </summary>
public string SelectProduct { get; set; }
/// <summary>
/// Gets or sets product name.
/// </summary>
public string ProductName { get; set; }
}
公共类产品到
{
公共产品
{
}
///
///获取或设置其新产品还是现有产品
///
公共字符串SelectProduct{get;set;}
///
///获取或设置产品名称。
///
公共字符串ProductName{get;set;}
}
请注意,更纯粹的方法是在控制器或视图模型中而不是在视图中构建
SelectList
。这样,您就可以将构建可能值的逻辑从视图本身中分离出来。如果您在ProductDto
类中使用的SelectList
是System.Web.Mvc.SelectList
,那么这就是问题的原因System.Web.Mvc.SelectList
没有无参数构造函数
@Html.DropDownListFor(p => p.SelectProduct, new SelectList(new[]
您应该更改此代码
@Html.DropDownListFor(p => p.SelectProductID, new SelectList(new[]
我使用了选择ProductID,因为当您想使用时。它将充满模型
因此,您只能选择dropdownlist中的一项
我希望这对你有用
谢谢。此代码将帮助您。使用此方法 Htmlcs
@model OspWebsite.Models.bcEvent
@using (Html.BeginForm())
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="col-4 ">
@Html.LabelFor(model => model.eventCountry, new { @class = "col-sm-6 control-label" }) //For Label
@Html.DropDownList("FeedBack", ViewData["dropdownCountry"] as List<SelectListItem>) //For DropDown
</div>
}
控制器
public class AdminEventsController : Controller
{
[HttpGet]
public ActionResult addEvent()
{
////ComboBox Country
List<SelectListItem> lst = new List<SelectListItem>();
MngEvent events = new MngEvent();
DataTable dt1 = events.ShowEvent();
for (int i = 0; i <= dt1.Rows.Count; i++)
{
lst.Add(new SelectListItem { Text = dt1.Rows[i]["ConfrenceCountry"].ToString(), Value = dt1.Rows[i]["ConfrenceID"].ToString() });
}
ViewData["dropdownCountry"] = lst;
return View();
}
}
公共类AdminEventsController:控制器
{
[HttpGet]
公共行动结果附录()
{
////组合框国家
List lst=新列表();
MngEvent事件=新的MngEvent();
数据表dt1=事件
public class MngEvent
{
public DataTable ShowEvent()
{
try
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ospDB"].ConnectionString);
SqlCommand cmd = new SqlCommand("usp_SelectAllConfrence", con);
SqlDataAdapter sda = new SqlDataAdapter();
DataTable dt = new DataTable();
cmd.CommandType = CommandType.StoredProcedure;
if (con.State.Equals(ConnectionState.Closed))
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
con.Close();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
}
public class AdminEventsController : Controller
{
[HttpGet]
public ActionResult addEvent()
{
////ComboBox Country
List<SelectListItem> lst = new List<SelectListItem>();
MngEvent events = new MngEvent();
DataTable dt1 = events.ShowEvent();
for (int i = 0; i <= dt1.Rows.Count; i++)
{
lst.Add(new SelectListItem { Text = dt1.Rows[i]["ConfrenceCountry"].ToString(), Value = dt1.Rows[i]["ConfrenceID"].ToString() });
}
ViewData["dropdownCountry"] = lst;
return View();
}
}