Asp.Net MVC 3自定义模型与变量对象的绑定

时间:2012-07-23 05:56:57

标签: c# asp.net-mvc model-binding

我的实体:( PersonModel应该有一个AddressOne或AddressTwo(可能还有其他)类型的地址,因此PersonModel有一个地址字段的对象类型。)

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public object Address { get; set; }
}

public class AddressOne
{
    public string Street { get; set; }
    public string City { get; set; }
}

public class AddressTwo
{
    public string Province { get; set; }
    public string State { get; set; }
}

模型:(我在typeOfAddress中传递一个隐藏字段,以便在表单提交后匹配正确的地址)

public class PersonModel
{
    private System.Type _typeOfAddress;
    private object _address;

    [Required]
    public int PersonId { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Surname { get; set; }

    public System.Type typeOfAddress
    {
        get { return _typeOfAddress; }
        set { _typeOfAddress = value; }
    }

    public object Address
    { 
        get {
            return _address;
        } 
        set {
            _address = value;
            _typeOfAddress = _address.GetType(); 
        } 
    }
}

public class AddressOneModel
{
    [Required]
    public string Street { get; set; }
    [Required]
    public string City { get; set; }
}

public class AddressTwoModel
{
    [Required]
    public string Province { get; set; }
    [Required]
    public string State { get; set; }
}

我的观点(对于地址字段,我在此代码中省略了广告编辑模板):

@using (Html.BeginForm()) {

<ul>
<li> 
    PersonId: @Html.EditorFor(model => model.PersonId)
</li>
<li>
    Name: @Html.EditorFor(model => model.Name)
</li>
<li>
    Surname: @Html.EditorFor(model => model.Surname)
</li>
<li>
    Address:
</li>
<li>
    @Html.HiddenFor(model => model.typeOfAddress)
    @Html.EditorFor(model => model.Address)
</li>
</ul>
<button type="submit">Submit</button>

}

然后我的控制器:(在这个例子中我在模型中加载了AddressOne,但应该是一个或两个取决于运行时......)

    [HttpGet]
    public ActionResult Index()
    {
        PersonModel myPerson = new PersonModel();

        myPerson.PersonId = 1;
        myPerson.Name = "Michael";
        myPerson.Surname = "Douglas";

        AddressOneModel Address = new AddressOneModel();
        Address.Street = "5th Avenue";
        Address.City = "New York";

        myPerson.Address = Address;

        return View(myPerson);
    }

    [HttpPost]
    public ActionResult Index([ModelBinder(typeof(PersonModelBinder))]PersonModel myPerson)
    {
        if (ModelState.IsValid) { 
            // some things here
        }
        return View();
    }

然后是PersonModel的Model Binder:

public class PersonModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        PersonModel bindedModel = new PersonModel();

        foreach (var Property in typeof(PersonModel).GetProperties())
        {
            PropertyInfo info = bindedModel.GetType().GetProperty(Property.Name); 
            object castedInfo = new object();

            var uType = info.PropertyType;
            if (uType == typeof(string))
            {
                castedInfo = bindingContext.ValueProvider.GetValue(Property.Name).AttemptedValue.ToString();
            }
            else if (uType == typeof(Type))
            {
                castedInfo = Type.GetType(bindingContext.ValueProvider.GetValue(Property.Name).AttemptedValue.ToString());
            }
            else if (uType == typeof(object))
            {
                string objType = bindingContext.ValueProvider.GetValue("typeOfAddress").AttemptedValue; 
                object address = (object)Activator.CreateInstance(Type.GetType(objType));

                // another foreach as previous
            }
            else
            {
                object uCasted = (object)Activator.CreateInstance(info.PropertyType);
                uCasted = Convert.ChangeType(bindingContext.ValueProvider.GetValue(Property.Name).AttemptedValue, Property.PropertyType);
                castedInfo = uCasted;
            }
            info.SetValue(bindedModel, castedInfo, null);
        }
        return bindedModel;
    }

这是实现PersonModel绑定的正确方法吗?那么[Post]控制器中的验证呢?

我还看到了一种以这种方式使用DefaultBinder的方法:

[ModelBinderType(typeof(PersonModel))]
public class PersonModelBinder : DefaultModelBinder
{
     //...
}

但我在MVC3中找不到任何ModelBinderType的引用!!有什么建议吗?

1 个答案:

答案 0 :(得分:2)

好像你正在努力做到这一点。你不应该需要一个模型绑定器。我要做的是将每种类型的地址添加到模型中,并在页面上显示非空的地址。这样可以省去很多麻烦。

public class PersonModel : IValidatableObject 
{
    private System.Type _typeOfAddress;
    private object _address;

    [Required]
    public int PersonId { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Surname { get; set; }

    public System.Type typeOfAddress
    {
        get { return (AddressOne ?? AddressTwo ?? AddressThree).GetType(); }
    }

    public AdressOneModel AddressOne {get;set;}
    public AdressTwoModel AddressTwo {get;set;}
    public AdressThreeModel AddressThree {get;set;}
}

然后可能在页面上执行此操作

@using (Html.BeginForm()) 
{
    <ul>
    <li> 
        PersonId: @Html.EditorFor(model => model.PersonId)
    </li>
    <li>
        Name: @Html.EditorFor(model => model.Name)
    </li>
    <li>
        Surname: @Html.EditorFor(model => model.Surname)
    </li>
    <li>
        Address:
    </li>
    <li>
    @if(model.AddressOne != null)
    {
        Html.EditorFor(model => model.AddressOne)
    }
    else if(model.AddressTwo != null)
    {
        Html.EditorFor(model => model.AddressTwo)
    }
    </li>
    </ul>
    <button type="submit">Submit</button>
}

但这实际上取决于你为什么要这样做