C# MVC No submit pass object between views
Asked Answered
M

1

2

I am sorry for my typos.I am working on proof of concept C# ASP.NET MVC application where I need to pass data between two views when there is no post and get. One view launches a modal dialog and I need communication between them. We are using JQuery.

I have a view called Charges.cshtml with a data grid. The first column of the datagrid may have span element or a link element depending on a property which will tell whether the charge have single or multiple descriptions. The view looks like below.

Charges

If the charge has multiple descriptions user will click the corresponding description link( Description2 in this case ) and a modal dialog will open showing various descriptions like below

Multiple Descriptions

Now in this modal dialog user will confirm/select one description. Now I need to close the modal dialog and update the description of selected charge like below

Updated Description

The hard part here is how to pass data between two views. I am ok to pass data via controller or via javascript.

I tried various ways to pass selected charge from Charges.cshtml to LoadLoanChargeDescriptions method in LoanCharge controller like json serialize, ViewData, ViewBag, TempData and so on but of no use. I can pass simple data types like int, string, float but not whole object. I feel I need to pass CurrentDescription and Descriptions to my controller and from their I need to move to other pieces. I tried to pass List of strings but could not see how to access them in controller since I got count as 0 in my controller. I am able to open popup of multiple descriptions UI ( for now just added Hello text )

Please see below for my code snippets

Charges.cshtml

@model ChargeViewModel
@using (Html.FAFBeginForm())
{
    <div>
            <table>
                <tbody>
                <tr >                   
                    //.....                     
                    <td>
                        @if(Model.IsMultipleMatch)
                        {
                            var loanCharge = Model as ChargeViewModel;
                            if (loanCharge.IsMultipleMatch == true)
                            {
                                //string vm = @Newtonsoft.Json.JsonConvert.SerializeObject(loanCharge);                                                                             
                                <span>
                                    <a 
onclick="ShowMatchingDescriptions('@Url.Action("LoadLoanChargeDescriptions", "LoanCharge")','', '920','500')">
                                        @loanCharge.Description
                                    </a>
                                </span>    
                             }                        
                        }
                        else
                        {
                            <span>Model.Description</span>
                        }  

                    </td>
                </tr>                     
                </tbody>    
            </table>
    </div> 
}

public class ChargeViewModel
{
  public string Description {get;set;}
  public bool IsMultipleMatch {get;set;}
  public List<string> Descriptions {get;set;}
}

public class LoanChargeController
{
  public ActionResult LoadLoanChargeDescriptions()
  {
      // get data here and pass/work on
      return View("_PartialMultipleMatchPopup", null);
  }
}

In Review.js

function ShowMatchingDescriptions(popUpURL, windowProperties, w, h) {
    try {                
        var left = (screen.width / 2) - (w / 2);
        var top = (screen.height / 2) - (h / 2);

        var properties = windowProperties + "dialogwidth:" + w + "px;dialogheight:" + h + "px;dialogtop:" + top + "px;dialogleft:" + left + "px;scroll:yes;resizable:no;center:yes;title:Matching Lender’s Fee;";
        $.when(
        window.showModalDialog(popUpURL, window, properties)
        )
        .then(function (result) {
            var childWindow = result;
        });
    }
    catch (err) {
        alert("Error : " + err)
    }
}

UPDATE 1

I updated my question and posted more details.

Thanks in advance.

UPDATE 2

Please see for my solution at below link.

MVC pass model between Parent and Child Window

Matriculation answered 1/4, 2016 at 3:33 Comment(0)
O
1

Why don't you use the AJAX for pass the data?

    function ChargeViewModel() {
        this.Description ='';
        this.IsMultipleMatch =false;

    }

    var chargeViewModel= new ChargeViewModel();
    var data = JSON.stringify({ 'chargeViewModel': chargeViewModel });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'html',
        type: 'POST',
        url: '@Url.Action("LoadLoanChargeDescriptions", "LoanChargeController")',
        data: data,
        success: function (result) {
           //result will be your partial page html output
        },
        failure: function (response) {

        }
    });

Then you have to change the controller like this:

public ActionResult LoadLoanChargeDescriptions(ChargeViewModel chargeViewModel)
  {
      // get data here and pass/work on
      return View("_PartialMultipleMatchPopup", null);
  }

Let me know you have queries..

Odelia answered 1/4, 2016 at 6:26 Comment(3)
@Siva..Thank you very much for your help. Actually recently our architecture team has made changes to our framework and $.ajax throws exception thats why I need to use ShowMatchingDescriptions() in my Review.js. Can you please let me know how can I do like thatMatriculation
@Matriculation . Sorry.I am not familiar with Review.jsOdelia
@ Siva.. I solved .. please see this for my solution... #36730067Matriculation

© 2022 - 2024 — McMap. All rights reserved.