Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm working on an asp-mvc application and facing the following issue: I have a model with simple properties plus one property which is a list of my custom object, and I render the Ienumerable property as mentioned here: Passing IEnumerable property of model to controller post action- ASP MVC

In my view, 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 already inserted data, so I need to pass the model to the corresponding action.

I've noticed that the model os transferred entirely only upon post. 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've read that assigning session in js is not recommended, but also tried TempData, and there the data was always null.

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

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

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 Controller:

public ActionResult AddBlankItemTemplate(FilterModel model)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

The line @{ Session["fromAddFullItem"] = "true";} is Razor code and will be run on page rendering and load regardless of where you put it in the page.

It's not client side code so won't wait for your js code to run. If you're trying to synchronise state between js and MVC have you looked into angularjs which could simplify these actions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...