gpt4 book ai didi

c# - mvc 中没有为此对象定义无参数构造函数

转载 作者:行者123 更新时间:2023-11-30 14:52:05 26 4
gpt4 key购买 nike

当我在 View 中使用 SelectList 时,出现错误“没有为此对象定义无参数构造函数”,我看到了很多解决方案,但无法找到适合我的解决方案。这是查看代码:

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

Controller :

[HttpPost]
public ActionResult Product(ProductDto model)
{
//code here
}

型号:

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; }

}

堆栈跟踪:

[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

谢谢

最佳答案

DropDownListFor 旨在为模型中的属性设置一个值 - 您的模型包含 SelectProduct 属性,类型为 SelectList -我猜它应该是 string 类型,因为这是您在 View 中构建的 SelectListValue 属性的类型。

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; }

}

请注意,更纯粹的方法是在 Controller 或 ViewModel 而不是在 View 中构建 SelectList。这样,您就可以将构建可能值的逻辑与 View 本身分开。

关于c# - mvc 中没有为此对象定义无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32524921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com