C#MVC没有提出意见之间传递对象 [英] C# MVC No submit pass object between views

查看:147
本文介绍了C#MVC没有提出意见之间传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很对不起我的typos.I凌晨概念C#ASP.NET MVC应用程序,我需要在这两个视图之间传递数据的证明时,没有职位和获得。一种观点启动一个模式对话框,我需要它们之间的通信。我们正在使用JQuery。

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.

我有一个名为Charges.cshtml与数据网格视图。 DataGrid的第一列可能有跨度元素或取决于一个链接元素
财产会告诉充电是否有单个或多个描述。视图看上去像下面。

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.

罪名

如果充电有多个描述用户点击相应的原文链接(广告描述在这种情况下)和模态对话框将打开,显示以下各种描述像

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

多描述

现在在这个模式对话框的用户将确认/选择一个描述。现在,我需要关闭模态对话框,并更新所选择的描述
充电像下面

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

更新说明

在这里难的是如何在两个视图之间传递数据。我行通过控制器或通过JavaScript来传递数据。

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

我试过各种方法从Charges.cshtml选择的电荷传递给LoadLoanChargeDescriptions方法LoanCharge控制器像JSON序列化,ViewData的,ViewBag,TempData的等等,但没有用的。我可以通过简单的数据类型,如整型,字符串,浮点而不是整个对象。我觉得我需要CurrentDescription和说明传递给我的控制器,并从他们我需要移动到其他部分。我试图通过字符串列表,但不能看到如何访问它们的控制器,因为我得到算作我的控制器0。我能够打开多个描述用户界面的弹出(现在刚添加文本Hello)

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 )

请参阅下面的我的code片段

Please see below for my code snippets

Charges.cshtml

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

在Review.js

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

更新1

我更新了我的问题,并发布更多细节。

I updated my question and posted more details.

先谢谢了。

更新2

请参阅我的解决方案在下面的链接。

Please see for my solution at below link.

<一个href=\"http://stackoverflow.com/questions/36729567/mvc-pass-model-between-parent-and-child-window\">MVC通过父母与子女窗口

推荐答案

你为什么不使用AJAX用于传递数据?

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

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

    }

    var chargeViewModel=fnChargeViewModel();
    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..

这篇关于C#MVC没有提出意见之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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