更复杂的(现实生活)modelbinding? [英] More complex(real-life) modelbinding?

查看:135
本文介绍了更复杂的(现实生活)modelbinding?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

卷与我想象下面的例子:

Roll with me and imagine the following example:

Public ViewResult GiveMeFruit(int personId, string personName, int personAge, int fruitId){
    Person person = PersonService.GetPerson(personId);
    person.Name = personName;
    person.Age = age;
    person.Fruits.Add(FruitService.GetFruit(fruitId));
    ViewData.Person = person;
    View(ViewData);
}

本应该做的更好,像这样

This should be done better like so

Public ViewResult GiveMeFruit(Person person, IFruit fruit){
    person.Fruits.Add(fruit);
    ViewData.Person = person;
    View(ViewData);
}

我想正确的modelbinding较早,但我不能让它正常工作。所有的例子告诉你它是一个非常简单的类型是如何工作的,从来没有与多个,复杂类型。将如何ModelBinder的知道是什么类型什么领域呢?如果有什么fruit1和fruit2?如何将粘结剂知道该怎么用具体的类型我IFruit接口? 此外,我不知道它是如何工作的,如果我想给一个IEnumerable水果给我的人。

I tried proper modelbinding earlier but I couldn't get it to work properly. All the examples show you how it works with one extremely simple type, never with multiple, complex types. How would the modelbinder know what field is for what type? What if there is a fruit1 and a fruit2? How would the binder know what concrete type to use for my IFruit interface? Furthermore I wonder how it would work if I want to give an IEnumerable fruits to my Person.

推荐答案

我相信它应该是这样的:

I believe it should go like this :

<input type="text" name="person.Name" value="" />
<input type="text" name="person.Age" value="" />
<input type="text" name="fruit.Property1" value="" />
<input type="text" name="fruit.Property2" value="" />

对于集合:

<input type="text" name="fruit[0].Property1" value="" />
<input type="text" name="fruit[0].Property2" value="" />
<input type="text" name="fruit[1].Property1" value="" />
<input type="text" name="fruit[1].Property2" value="" />

就像<一href="http://stackoverflow.com/questions/711184/asp-net-mvc-example-of-editing-multiple-child-records/712817#712817">this问题。

这篇关于更复杂的(现实生活)modelbinding?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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