【问题标题】:dropdown value null when using, viewmodel & modelbinder in asp.net mvc在 asp.net mvc 中使用 viewmodel 和 model binder 时下拉值 null
【发布时间】:2009-07-20 18:16:40
【问题描述】:

从视图发布时,我正在使用 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));
        }

查看:

<%@ Import Namespace="BlackDiamond.Buzz.MVCWeb.Controllers"%>
<%@ Import Namespace="BlackDiamond.Buzz.Core"%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CustomerViewModel>" %>
<%
    Customer customer = ViewData.Model.Customer;
    using (Html.BeginForm())
    {
    %>    
    <table>
        <tr>
            <td>Local Store:</td>
            <td><%= Html.DropDownList("LocalStore", ViewData.Model.Stores)%></td>
         </tr>
         <tr>
            <td>Name:</td><td><%= Html.TextBox("Name", customer.Name)%></td>
         </tr>
         <tr>
            <td>Email:</td><td><%= Html.TextBox("Email", customer.Email)%></td>
        </tr>
        <tr>
            <td>Mobile:</td><td><%= Html.TextBox("Mobile", customer.Mobile)%></td>
        </tr>
    </table>
    <input type="submit" value="Create" />
<%}%>

【问题讨论】:

    标签: asp.net-mvc modelbinders


    【解决方案1】:

    可能是因为您将 LocalStore 声明为 Store 类型?

    public virtual Store LocalStore { get; set; }
    

    我认为它应该是 int(如果“id”属性是 int)或字符串。不过不确定。

    public virtual int LocalStore { get; set; }
    

    【讨论】:

    • 感谢您的帖子。问题确实是 LocalStore 值是 Store 类型的对象,而不是字符串 guid。必须创建一个自定义模型绑定器以使用 guid id 检索正确的商店。再次感谢您的帖子
    【解决方案2】:

    必须创建一个自定义模型绑定器来根据下拉列表中的 guid 检索 Store 实体:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多