Frage

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});
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top