简体   繁体   English

C# MVC 视图之间没有提交传递对象

[英]C# MVC No submit pass object between views

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.我很抱歉我的错别字。我正在研究 C# ASP.NET MVC 应用程序的概念证明,当没有发布和获取时,我需要在两个视图之间传递数据。 One view launches a modal dialog and I need communication between them.一个视图启动一个模态对话框,我需要它们之间的通信。 We are using JQuery.我们正在使用 JQuery。

I have a view called Charges.cshtml with a data grid.我有一个名为 Charges.cshtml 的视图,带有一个数据网格。 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.数据网格的第一列可能有 span 元素或 link 元素,这取决于一个属性,该属性将判断费用是具有单个还是多个描述。 The view looks like below.视图如下所示。

收费

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如果费用有多个描述,用户将单击相应的描述链接(在本例中为描述 2),将打开一个模态对话框,显示如下所示的各种描述

多重描述

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现在我需要关闭模态对话框并更新所选费用的描述,如下所示

更新说明

The hard part here is how to pass data between two views.这里的难点在于如何在两个视图之间传递数据。 I am ok to pass data via controller or via javascript.我可以通过控制器或通过 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.我尝试了各种方法将 Charges.cshtml 中的选定费用传递到 LoanCharge 控制器中的 LoadLoanChargeDescriptions 方法,如 json serialize、ViewData、ViewBag、TempData 等,但没有用。 I can pass simple data types like int, string, float but not whole object.我可以传递简单的数据类型,如 int、string、float,但不能传递整个对象。 I feel I need to pass CurrentDescription and Descriptions to my controller and from their I need to move to other pieces.我觉得我需要将 CurrentDescription 和 Descriptions 传递给我的控制器,并且我需要从它们转移到其他部分。 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.我试图传递字符串列表,但无法看到如何在控制器中访问它们,因为我在控制器中被计数为 0。 I am able to open popup of multiple descriptions UI ( for now just added Hello text )我能够打开多个描述 UI 的弹出窗口(现在只添加了 Hello 文本)

Please see below for my code snippets请参阅下面的我的代码片段

Charges.cshtml费用.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在 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更新 1

I updated my question and posted more details.我更新了我的问题并发布了更多详细信息。

Thanks in advance.提前致谢。

UPDATE 2更新 2

Please see for my solution at below link.请在下面的链接中查看我的解决方案。

MVC pass model between Parent and Child Window 父子窗口之间的MVC传递模型

Why don't you use the AJAX for pass the data?为什么不使用 AJAX 来传递数据?

    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..让我知道你有疑问..

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

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