简体   繁体   English

MVC问题中的模型绑定器

[英]Model binder in MVC question

I am getting the following Error The model item passed into the dictionary is of type 'MvcWebApplication.Models.Product' but this dictionary requires a model item of type 'MvcWebApplication.ViewModels.ProductCommonViewModel'. 我收到以下错误传递到字典中的模型项类型为'MvcWebApplication.Models.Product',但此字典需要类型为'MvcWebApplication.ViewModels.ProductCommonViewModel'的模型项。

Controller Code is as follows:- 控制器代码如下: -

public ActionResult Index([Bind(Prefix= "MvcWebApplication.ViewModels.ProductCommonViewModel")] Product product)
View Model class sent to the View:
namespace MvcWebApplication.ViewModels
{
    public class ProductCommonViewModel
    {
        public Product Product { get; set; }
    }
}

Model Binder : 型号粘合剂:

namespace MvcWebApplication.ModelBinders
{
    public class ProductBinder : IModelBinder
    {
        #region IModelBinder Members

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase fc = controllerContext.HttpContext.Request;

            Product product = new Product();
            LaunchSheet launchSheet = null; ;
            ProductPricing productPricing = null;

            product.LoanTypeId = Convert.ToInt32(fc["rblLoanType"]);
            product.ProductTypeId = Convert.ToInt32(fc["ddlproductType"]);
etc...
            return Product;
          }
      }
}

Product Class 产品类别

[ModelBinder(typeof(ProductBinder))]
    public partial class Product : IRuleEntity
    {

}

What am I doing wrong? 我究竟做错了什么?

It's not the binding which is broken. 这不是破坏的约束力。 The problem is that your strongly-typed view (aspx/ascx) requires a model of type MvcWebApplication.ViewModels.ProductCommonViewModel, but the action which renders it is passing a model of type MvcWebApplication.Models.Product. 问题是您的强类型视图(aspx / ascx)需要MvcWebApplication.ViewModels.ProductCommonViewModel类型的模型,但呈现它的操作是传递MvcWebApplication.Models.Product类型的模型。 Note that this has nothing to do with the code in your question; 请注意,这与您问题中的代码无关; the bug is in the "return View(model)" line elsewhere. 该bug位于其他地方的“返回视图(模型)”行中。

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

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