Asp.net MVC Composite类自定义模型绑定

时间:2022-11-30 16:36:49

I've searched all over the web but not finding the solution for the following problem:

我在网上搜索但没有找到以下问题的解决方案:

Say I have three ViewModel classes

假设我有三个ViewModel类

public class ViewModelNewPerson
{
    public string PersonName;
    public string Address;
    public string EyeColor;
    //etc
}

public class ViewModelSelectPerson
{
    public int SelectedPersonId;
}

public class ViewModelComposite
{
    public ViewModelSelectPerson selectViewModel;
    public ViewModelNewPerson newPersonViewModel;
}

and I want to do the following things:

我想做以下事情:

In the Controller I want to create a GET Action which uses the class ViewModelComposite as its Get model, and in the view I want the user choose from the following two available actions: to choose a existed person, and to add a new person as the selected value.

在Controller中我想创建一个使用ViewModelComposite类作为其Get模型的GET Action,并且在视图中我希望用户从以下两个可用操作中进行选择:选择一个已存在的人,并添加一个新人作为选定的价值。

So I need to create two forms in the View, and there would be two POST Actions added to the Controller using the Post model of class ViewModelNewPerson and ViewModelSelectPerson.

所以我需要在View中创建两个表单,并且使用ViewModelNewPerson和ViewModelSelectPerson类的Post模型将两个POST操作添加到Controller。

My question is, how can I do the manual model binding using a Custom Model Binder that can convert the Composite class of ViewModelComposite to ViewModelNewPerson in the Action of create a new person, and to ViewModelSelectPerson in the Action of select an existing person?

我的问题是,如何使用自定义模型绑定器进行手动模型绑定,可以在创建新人的操作中将ViewModelComposite的Composite类转换为ViewModelNewPerson,并在选择现有人的操作中将ViewModelSelectPerson转换为ViewModelSelectPerson?

EDIT:

Now I have an idea of decomposing the class ViewModelComposite and declare every property in the two classes into the composite class, and the default model binder will do the trick, I think. But that'll drop the composite pattern, and is not something I wanted.

现在我有了分解ViewModelComposite类的想法,并将两个类中的每个属性声明为复合类,我认为默认的模型绑定器可以解决这个问题。但这会降低复合模式,而不是我想要的东西。

1 个解决方案

#1


0  

You would use one single view model in your form, you would have a post action that receives your single view model.

您将在表单中使用一个单一视图模型,您将有一个接收单个视图模型的post操作。

In your Controller code:

在您的Controller代码中:

public ActionResult GetSomeData(MyCustomViewModel  model){
      // add the first element
      var person = Person.Add(model.Person);
      // update the second object in model, with related / needed ID
      model.PersonContent.PersonId = person.id;
      // add in related content
      var AddedContent = PersonContent.Add(model.PersonContent);
}

single form, multiple actions, multiple tables

单个表单,多个操作,多个表

#1


0  

You would use one single view model in your form, you would have a post action that receives your single view model.

您将在表单中使用一个单一视图模型,您将有一个接收单个视图模型的post操作。

In your Controller code:

在您的Controller代码中:

public ActionResult GetSomeData(MyCustomViewModel  model){
      // add the first element
      var person = Person.Add(model.Person);
      // update the second object in model, with related / needed ID
      model.PersonContent.PersonId = person.id;
      // add in related content
      var AddedContent = PersonContent.Add(model.PersonContent);
}

single form, multiple actions, multiple tables

单个表单,多个操作,多个表