将信息从视图传输到控制器 [英] Transferring info from view to Controller

查看:39
本文介绍了将信息从视图传输到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理asp-mvc应用程序,并且遇到以下问题:我有一个具有简单属性的模型,外加一个属性(这是我的自定义对象的列表),并且呈现了Ienumerable属性,如下所示:将模型的IEnumerable属性传递给控制器发布操作-ASP MVC

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

在我看来,我有一个按钮,应该将项目添加到模型的ienumerable属性中.当然,我不想丢失已经插入的数据,因此需要将模型传递给相应的操作.

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);
        }

我读到不建议在js中分配会话,但也尝试过TempData,并且那里的数据始终为空.

I've read that assigning session in js is not recommended, but also tried TempData, and there the data was always null.

我的问题是,即使我来自另一个按钮,Session ["fromAddFullItem"]始终为true.如果我将断点放在addbtn中,请单击Session- Session ["fromAddFullItem"] ="false" ;,然后按另一个按钮,即使没有按下添加键,我也会发现由于某种奇怪的原因,该断点被击中了按钮.

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.

有帮助吗?也许还有另一种方式来实现我想要的.当前,无论我按哪个按钮(张贴表单),它都以Session ["fromAddFullItem"] ="false"出现,并执行操作AddBlankItemTemplate.谢谢.

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.

编辑-AJAX帖子

    $(".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;
    });

和控制器

public ActionResult AddBlankItemTemplate(string modelData)

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

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

和控制器:

public ActionResult AddBlankItemTemplate(FilterModel model)

推荐答案

@ {Session ["fromAddFullItem"] ="true";} 是Razor代码,将在页面渲染中运行并加载,无论您将其放置在页面中的什么位置.

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.

这不是客户端代码,因此不会等待您的js代码运行.如果您要在js和MVC之间同步状态,您是否研究过可以简化这些操作的angularjs.

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.

这篇关于将信息从视图传输到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆