简体   繁体   English

对象作为动作控制器的参数?

[英]Object as a parameter for an action controller?

Is it possible for an action controller to accept a literal object. 动作控制器是否可以接受文字对象。 For example, I have several views in which I would like to post various models from to a single controller that can then determine the incoming model object for further processing. 例如,我有几个视图,我想将各种模型发布到单个控制器中,然后可以确定传入的模型对象以进行进一步处理。

Model sample: 模型样本:

public class Model1
{
   // properties, etc.
}

public class Model2
{
   // properties, etc.
}

public class Model3
{
   // properties, etc.
}

controller sample: 控制器样本:

[HttpPost]
public ActionResult ProcessModel(Object anyModel)
{
   // determine the model
   if((anyModel as Model1) != null)
   {
     var model1 = anyModel as Model1;
     // continue with code
   }
   else if((anyModel as Model2) != null)
   {
     var model2 = anyModel as Model2;
     // continue with code
   }
   // continue with model check, etc.       
}

I've tried, but my controller does not appear to be picking up the model as my object parameter remains empty. 我已经尝试过了,但是由于我的对象参数仍然为空,所以控制器似乎没有拾取模型。 Is this possible? 这可能吗?

Have a quick read about how model binding works ... The model binder (which takes whatever is posted to your Action and turns it into the anyModel parameter uses the type of the parameter to determine what to do. 快速了解模型绑定的工作原理 ... 模型绑定器(它将任何内容发布到Action并将其转换为anyModel参数)使用参数的类型来确定要执行的操作。

Since the type is Object it can't do anything. 由于类型是Object因此它无法执行任何操作。

My guess (depending on what you're trying to achieve) is that you can have several Action overloads each with a different type of Model as the parameter which then call common code. 我的猜测(取决于您要实现的目标)是,您可以有多个Action重载,每个重载都使用不同类型的Model作为参数,然后调用通用代码。

[HttpPost]
public ActionResult ProcessModel(Model1 anyModel){}

[HttpPost]
public ActionResult ProcessModel(Model2 anyModel){}

[HttpPost]
public ActionResult ProcessModel(Model3 anyModel){}

That said it's a bit odd to have one action which takes lots of different models. 那就是说采取一个动作需要很多不同的模型有点奇怪。 There's a good chance you're better off doing something else. 您很有可能要做其他事情。

Your question might gather a better answer if you say what you're trying to achieve 如果您说出自己要实现的目标,那么您的问题可能会得到更好的答案

The Default Asp.NET ModelBinder cannot bind generic Objects this way. 默认的Asp.NET ModelBinder无法以这种方式绑定通用对象。 You should take a look here to understand how the model will be build back in the server by the DefaultModelBinder: Understanding ASP.NET MVC Model Binding . 您应该在这里了解一下如何通过DefaultModelBinder在服务器中重新构建模型: 了解ASP.NET MVC模型绑定

Given that your form has many Models, you should encapsulate them into a ViewModel to do this kind of operation. 鉴于您的表单有许多模型,您应该将它们封装到ViewModel中以执行这种操作。

The ViewModel should looks like this: ViewModel应该看起来像这样:

public class MyViewModel
{
  public Model1 Model1 {get; set;}
  public Model1 Model2 {get; set;}
  public Model1 Model3 {get; set;}
}

And the controller: 和控制器:

[HttpPost]
public ActionResult ProcessModel(MyViewModel myViewModel)
{
  // determine the model
  if(myViewModel.Model1 != null)
  {
    // continue with code
  }
  else if(myViewModel.Model2 != null)
  {
    // continue with code
  }
  // continue with model check, etc.       
}

Recently I faced the same issue and resolved it as below: 最近,我遇到了相同的问题,并通过以下方法解决了该问题:

Step 1: From javascript pass 2 parameter : 步骤1:从javascript pass 2参数开始:

First, pass model name as String for identification which model is coming 首先,将模型名称作为字符串传递,以识别即将到来的模型

Second, Pass data from javascript using JSON.stringify(data) . 其次,使用JSON.stringify(data)从javascript传递数据。 where your data can be from Model1, Model2 , Model3 etc. 您的数据可以来自Model1, Model2 , Model3等。

Step2: In your controller: 步骤2:在您的控制器中:

[HttpPost]
public ActionResult ProcessModel(string modelName, string anyModel)
{
   switch(modelName)  {
      case "Model1":
             var modelValue= JsonDeserialize<Model1>(anyModel);
              // do something 
           break;
     case "Model2": 
            var modelValue= JsonDeserialize<Model2>(anyModel); 
            // do something
           break;
    }
}

You Need One method like below: 您需要一种如下所示的方法:

public T JsonDeserialize<T>(string jsonModel){
return JsonConvert.DeserializeObject<T>(jsonModel, jsonSettings);
}

JsonConvert need namespace "Newtonsoft.Json". JsonConvert需要命名空间“ Newtonsoft.Json”。

You also need to declare jsonSettings as below 您还需要如下声明jsonSettings

         JsonSerializerSettings jsonSettings= new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All,
        DefaultValueHandling = DefaultValueHandling.Ignore
    };

This solution is kind of workaround. 此解决方案是一种解决方法。 There is one more solution. 还有另一种解决方案。 you can check that also: How can I make a Controller Action take a dynamic parameter? 您还可以检查以下内容: 如何使Controller Action接受动态参数?

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM