Asp.net mvc 使用时下拉值为空,viewmodel&;asp.net mvc中的modelbinder

Asp.net mvc 使用时下拉值为空,viewmodel&;asp.net mvc中的modelbinder,asp.net-mvc,modelbinders,Asp.net Mvc,Modelbinders,从视图发布时,我使用asp.net的modelbinder功能将表单值绑定到实体 html在初始视图中以正确的选项和值项正确呈现 填写表单并过账时,除下拉列表中的值外,所有值都正确填充到实体中。不知道我做错了什么 代码如下: 客户实体: public class Customer : EntityBase { public virtual string Name { get; set; } public virtual string Email { ge

从视图发布时,我使用asp.net的modelbinder功能将表单值绑定到实体

html在初始视图中以正确的选项和值项正确呈现

填写表单并过账时,除下拉列表中的值外,所有值都正确填充到实体中。不知道我做错了什么

代码如下:

客户实体:

 public class Customer : EntityBase
    {
        public virtual string Name { get; set; }
        public virtual string Email { get; set; }
        public virtual string Mobile { get; set; }
        public virtual Store LocalStore { get; set; }
        public virtual DateTime? DateOfBirth { get; set; }

        public Customer(){}

        public Customer(string name, string email, string mobile, Store localStore):this(name, email, mobile, localStore, null)
        {
        }

        public Customer(string name, string email, string mobile, Store localStore, DateTime? dateOfBirth)
        {
            Name = name;
            Email = email;
            Mobile = mobile;
            LocalStore = localStore;
            DateOfBirth = dateOfBirth;
        }
    }
视图模型:

 public class CustomerViewModel {

        // Properties
       private IStoreRepository _StoreRepository;
       public Customer Customer { get; private set; }
       public SelectList Stores { get; private set; }

        // Constructor
        public CustomerViewModel(IStoreRepository storeRepository, Customer customer)
        {
            _StoreRepository = storeRepository;
            Customer = customer;
                Stores = new SelectList(_StoreRepository.GetAllStores(), "Id", "Name", Customer.LocalStore.Id);

        }
    }
控制器:

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Prefix="")]Customer customer)
        {
            return View(new CustomerViewModel(_StoreRepository, customer));
        }
视图:


本地商店:
姓名:
电邮:
流动电话:

可能是因为您将LocalStore声明为存储类型

public virtual Store LocalStore { get; set; }
我认为应该是int(如果“id”属性是int)或string。不过我不确定

public virtual int LocalStore { get; set; }

必须创建自定义modelbinder以根据下拉列表中的guid检索存储实体:

    public class CustomerModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType == typeof(Customer))
        {
            // get values
            string name = bindingContext.ValueProvider["Name"].AttemptedValue;
            string email = bindingContext.ValueProvider["Email"].AttemptedValue;
            string mobile = bindingContext.ValueProvider["Mobile"].AttemptedValue;
            Guid storeId = new Guid(bindingContext.ValueProvider["LocalStore"].AttemptedValue);
            Store localStore = IoC.Container.Resolve<IStoreRepository>().GetStore(storeId);

            // hydrate
            return new Customer(name, email, mobile, localStore);
        }
        else
        {
            return null;
        }
    }
}
公共类CustomerModelBinder:IModelBinder
{
公共对象绑定模型(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
if(bindingContext.ModelType==typeof(客户))
{
//获取价值
字符串名称=bindingContext.ValueProvider[“名称”]。AttemptedValue;
字符串email=bindingContext.ValueProvider[“email”].AttemptedValue;
字符串mobile=bindingContext.ValueProvider[“mobile”].AttemptedValue;
Guid storeId=新Guid(bindingContext.ValueProvider[“LocalStore”].AttemptedValue);
Store localStore=IoC.Container.Resolve().GetStore(storeId);
//水合物
返回新客户(姓名、电子邮件、手机、本地商店);
}
其他的
{
返回null;
}
}
}

谢谢你的帖子。问题确实是LocalStore值是Store类型的对象,而不是字符串guid。必须创建自定义modelbinder才能使用guid id检索正确的存储。再次感谢您的帖子
    public class CustomerModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType == typeof(Customer))
        {
            // get values
            string name = bindingContext.ValueProvider["Name"].AttemptedValue;
            string email = bindingContext.ValueProvider["Email"].AttemptedValue;
            string mobile = bindingContext.ValueProvider["Mobile"].AttemptedValue;
            Guid storeId = new Guid(bindingContext.ValueProvider["LocalStore"].AttemptedValue);
            Store localStore = IoC.Container.Resolve<IStoreRepository>().GetStore(storeId);

            // hydrate
            return new Customer(name, email, mobile, localStore);
        }
        else
        {
            return null;
        }
    }
}