当使用viewmodel&时下拉列表值为null在ASP.NET MVC中的modelbinder [英] dropdown value null when using, viewmodel & modelbinder in asp.net mvc

查看:75
本文介绍了当使用viewmodel&时下拉列表值为null在ASP.NET MVC中的modelbinder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using asp.net's modelbinder functionality to bind form values to my entity when posting from a view.

使用正确的选项和值项,html可以在初始视图中正确呈现.

The html renders correctly in the initial view with correct option and value items.

完成表单并过帐后,除下拉列表中的值外,所有值均正确填充到实体中.不知道我在做什么错.

When completing the form and posting, all values are populated correctly into the entity except the value from the dropdown list. not sure what I am doing wrong.

下面附有代码:

客户实体:

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

ViewModel:

ViewModel:

 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" />
<%}%>

推荐答案

必须创建一个自定义modelbinder才能根据下拉列表中的guid检索Store实体:

Had to create a custom modelbinder to retrieve the Store entity based on the guid from the dropdown list:

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

这篇关于当使用viewmodel&amp;时下拉列表值为null在ASP.NET MVC中的modelbinder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆