2

I'm developping an application based on asp.NET MVC 5

This Application downloads a big deal of data from a local webservice and should display it. Because it is much data, I call the webservice through a partial view, like this:

<div class="partialContents" id="partialContent" data-url="/Home/Action">
   Loading...
</div>

This is loaded asynchronous with the following JS-Code:

$(document).ready(function (e) {
        $(".partialContents").each(function (index, item) {
            var url = $(item).data("url");
            if (url && url.length > 0) {
                $(item).load(url);
            }
        });
    });

This works like intended.

But now I have to pass the values of a form to this partial view. I know I can pass arguments as usual, like a request to /Home/Action/Params but I have to pass many arguments of which some might not be set or empty or null. Because of this, I looked for a possibility to pass a object or a value of the ViewBag or something like this to the partial view.

Is there a possibility to pass a Model-Object to the Controller in my code? The data has to go to the controller for further validation and so on. I know I can pass a model to the view itself, but I need those data in my controller.

My controller is accessed as following:

public ActionResult Index()
    {
        //Do things
        return View(model);
    }

public ActionResult Action()
    {
        //Validate the Form-Data
        //Download Data from Webservice
        return PartialView("MyPartialView", model);
    }

Any help or tipps would be greatly appreciated.

Maybe I have to edit the Javascript-Code, but I don't usually code in JS, so I have the code from following source (followed his tutorial): blog.michaelckennedy.net

//Additional Info

My ViewModel looks like this:

public class PostForm
    {
        public string DatumVon { get; set; }
        public string DatumBis { get; set; }
        public string Werk { get; set; }
        public string Pruefplatz { get; set; }
        public string Auftrag { get; set; }
        public string Gueltig { get; set; }
    }

and my Java-Request like this:

$(item).load(url, {"DatumVon":dateVon, "DatumBis":dateBis, "Werk":werk, "Pruefplatz":pruefplatz, "Auftrag":auftrag, "Gueltig":gueltig});
UeliDeSchwert
  • 1,146
  • 10
  • 23

1 Answers1

1

If you are having a lot of parameters to pass to the action method, create a model which encompasses these parameters. For example, I have created MyParamModel class here below:

 public class MyParamModel
 {
    //Your parameters go here
    public int IntProperty { get; set; }
    public string StringProperty { get; set; }
 }

Then modify the action method to accept this class as parameter.

public ActionResult Action(MyParamModel model)

Modify the load call in the script to pass information to the url like this.

$(item).load(url, {"IntProperty":1, "StringProperty": "test"});

Once you have the model on the controller end, you can validate it accordingly. Hope this helps. If it does, mark this as answer.

  • Thanks for your answer. It seems like this could work. Because of the easter-weekend I'll try your solution on wednesday. If it works, I'll mark your solution as correct ;) – UeliDeSchwert Apr 17 '14 at 13:41
  • I tried your solution. If I make it exactly like you, it terminates and tells me `No parameterless constructor defined for this object.` But in the Request-Body, I see the data. So I tried to get to it by using the `Request.Form`-Collection, but this only returns, um, nothing. Not even an empty string nor null. – UeliDeSchwert Apr 23 '14 at 11:06
  • Can you provide the code of your view model? I am expecting that your model does not have a constructor with no parameters. – Phani Vankadari Apr 23 '14 at 13:06
  • Phani Vankadari: I updated my question to reflect your requirements. A few seconds ago, I fixed the issue: It was, because my Controller was implemented with the Parameter: `public ActionResult Action(PostForm form)` but it wanted a method without parameters like `public ActionResult Action()` Do you know, why this happens? – UeliDeSchwert Apr 23 '14 at 13:13
  • @stueliueli: These are the links for the error you described. Check if they are helpful. http://stackoverflow.com/questions/1355464/asp-net-mvc-no-parameterless-constructor-defined-for-this-object and http://stackoverflow.com/questions/16759296/no-parameterless-constructor-defined-for-this-object-in-mvc4 – Phani Vankadari Apr 23 '14 at 13:44
  • With this links, I was able to fix it. Your answer was correct. Thank you. – UeliDeSchwert Apr 25 '14 at 07:45