Passing information from view to controller

I am working on an asp-mvc application and am facing the following problem: I have a model with simple properties plus one property that is a list of my custom object and I am providing an Ienumerable property as stated here: Passing a model's IEnumerable property to a controller post action - ASP MVC

In my opinion, I have a button that is supposed to add items to the ienumerable property of my model. Of course, I don't want to lose the data already inserted, so I need to pass the model to the appropriate action.

I noticed that the os model is completely portable only to mail. So I did something like:

 $(".addButton").click(function (event) {
        event.preventDefault();
        $("#FilterForm").submit();
        @{ Session["fromAddFullItem"] = "true";}
        return false;
      });

      

And then in my controller I do something like:

public ActionResult Index(FilterModel model)
    {
        if (Session["fromAddFullItem"].ToString() == "true")
        {
            Session["fromAddFullItem"] = "false";
            return AddBlankItemTemplate(model);
        }

      

I read that assigning a session in js is not recommended but also tried TempData and the data was always empty there.

My problem is that Session ["fromAddFullItem"] is always right, even when I come from another button. If I put a breakpoint in addbtn, click in the -Session line ["fromAddFullItem"] = "false"; and click another button, I see that for some odd reason the mentioned breakpoint hit, even though I didn't click the add button.

Any help? Perhaps there is another way to achieve what I want. Currently, no matter which button I click (which submits the form) it comes in as Session ["fromAddFullItem"] = "false" and goes to the AddBlankItemTemplate action. Thank.

EDIT - AJAX POST

    $(".addButton").click(function(event) {
        event.preventDefault();
        var modelData = JSON.stringify(window.Model);
        $.ajax({
            url:  '@Url.Action("AddBlankItemTemplate")',
            type: 'POST',
            dataType: 'json',
            data: modelData,
            contentType: 'application/json; charset=utf-8',

        });
        return false;
    });

      

and controller

public ActionResult AddBlankItemTemplate(string modelData)

      

EDIT 2:

       $(".addButton").click(function (event) {
        event.preventDefault();
        $.ajax({
            url: '@Url.Action("AddBlankItemTemplate")',
            data: $("#FilterForm").serialize()
        }).success(function(partialView) {

            $('DetailsTemplates').append(partialView);
        });
    });

      

and the controller:

public ActionResult AddBlankItemTemplate(FilterModel model)

      

0


source to share


1 answer


The string @{ Session["fromAddFullItem"] = "true";}

is Razor code and will execute on the render and load page no matter where you placed it on the page.



This is not client-side code, so don't wait for your js code to run. If you are trying to sync state between js and MVC, you looked into angularjs that could make this easier.

+2


source







All Articles