简体   繁体   中英

Append values to ViewModel List MVC

I am developing MVC application. 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:

$(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:

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. This pattern prevent creating new instance of class object if it is not 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

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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