简体   繁体   English

将值附加到ViewModel列表MVC

[英]Append values to ViewModel List MVC

I am developing MVC application. 我正在开发MVC应用程序。 I have a Main Page which opens a popup on click on the hyperlink. 我有一个主页,单击超链接后会打开一个弹出窗口。

Main Page: 主页:

View 视图

<a href="#" id="CreateAddlField">Add Fields</a>

Javascript code to open the popup: 打开弹出窗口的Javascript代码:

$(function () {
    $('#CreateAddlField').click(function () {
        debugger;
        var Param2 = $("#Param2").val();
        var Param3 = "CreateFields";
            debugger;
            var div2 = $("#DivAddlFields");
            div2.load("/ControllerName/ActionName", { Param1: 0, Param2: Param2, Param3: Param3 }),
            div2.dialog({
                    modal: true,
                    width: 600,
                    height: 600,
                    title: "Add Fields",
                    resizable: false,
                    close: function (event, ui) {
                        location.reload();
                    }

                });

    });
});

This loads the popup. 这将加载弹出窗口。 The view of the Popup has some fields for entering values which is bound to the modal: 弹出窗口的视图具有一些用于输入绑定到模态的值的字段:

Main Modal: 主要模式:

public class MainModal
{
    public List<ListModal> lstModal { get; set; }
}

public class ListModal
{
    public string Param1 { get; set; }
    public string Param2 { get; set; }
    public string Param3 { get; set; }
    public string Param4 { get; set; }
    public int Param5 { get; set; }
    public int Param6 { get; set; }
    public string Param7 { get; set; }
}

On click of "Add" button in the popup activity it goes back to the Main Page and post all the Modal values to the controller. 在弹出活动中单击“添加”按钮时,它会返回到主页并将所有模态值发布到控制器。 Each time it does it is updating the list and appending it. 每次执行时,都会更新列表并追加列表。 How can I make the List to append its values? 如何使列表附加值?

Each time add button is clicked the following javascript is called and it goes back to the main page, where I want to append the list Model: 每次单击添加按钮时,都会调用以下javascript,它会返回主页,我要在其中添加模型列表:

function AddAddlFields()
{
    var formContainer = $("#FormID");
    var vmListModal = formContainer.serialize();
    window.open("/ControllerName/ActionName?vmListModal=" + formContainer);
}

In Controller: 在控制器中:

public PartialViewResult ActionName(ListModal vmListModal)
{ 
    MainModal vmMaimModal = new MainModal();
    vmMaimModal.lstModal.Add(vmListModal); //This is updating the list not appending it
    return PartialView("MainPage", vmMaimModal);
}

How can I append the values to the list modal, so that I can get all the values entered in the popup activity and display it in the main Page in table format? 如何将这些值附加到列表模式,以便获得在弹出活动中输入的所有值,并以表格格式在主页面中显示?

Every Time the action method loads in controller the previous values of model are lost because controller class reinitialize and all the classes again create with new instance, so you need to save them in a session or use Singleton pattern. 每次将操作方法​​加载到控制器中时,模型的先前值都会丢失,因为控制器类会重新初始化,并且所有类都会再次使用新实例创建,因此您需要将它们保存在会话中或使用Singleton模式。 This pattern prevent creating new instance of class object if it is not null. 如果该模式对象不为null,则它会阻止创建新的类对象实例。

Problem is with 问题出在

  MainModal vmMaimModal = new MainModal();

this new keyword creating new instance of object. 这个新关键字创建对象的新实例。

Example: 例:

Singleton Pattern 单例模式

Singleton Method 单例法

Create Singleton class of your model like this 像这样创建模型的Singleton类

 public sealed class Singleton
{

    private static MainModal MainInstance = null;

    private static readonly object padlock = new object();

    Singleton()
    {
    }



    public static MainModal MainInstance
    {
        get
        {
            lock (padlock)
            {
                if (MainInstance == null)
                {
                    MainInstance = new MainModal();
                }
                return MainInstance;
            }
        }
    }



}

Call this class in your controller like 在您的控制器中调用此类

  MainModal userinfo = Singleton.MainInstance;

Using Session 使用会议

Storing object in session 在会话中存储对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM