Object as a parameter for an action controller?

前端 未结 3 419
走了就别回头了
走了就别回头了 2021-01-28 09:21

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 th

相关标签:
3条回答
  • 2021-01-28 09:49

    Recently I faced the same issue and resolved it as below:

    Step 1: From javascript pass 2 parameter :

    First, pass model name as String for identification which model is coming

    Second, Pass data from javascript using JSON.stringify(data). where your data can be from Model1, Model2 , Model3 etc.

    Step2: In your controller:

    [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".

    You also need to declare jsonSettings as below

             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?

    Hope this helps.

    0 讨论(0)
  • 2021-01-28 10:03

    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.

    Since the type is Object it can't do anything.

    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.

    [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

    0 讨论(0)
  • 2021-01-28 10:05

    The Default Asp.NET ModelBinder cannot bind generic Objects this way. 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.

    Given that your form has many Models, you should encapsulate them into a ViewModel to do this kind of operation.

    The ViewModel should looks like this:

    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.       
    }
    
    0 讨论(0)
提交回复
热议问题