Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

c# - Transferring info from view to Controller

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...