C# MVC 视图之间没有提交传递对象 [英] C# MVC No submit pass object between views

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

问题描述

我很抱歉我的错别字.我正在研究 C# ASP.NET MVC 应用程序的概念证明,我需要在没有 post 和 get 时在两个视图之间传递数据.一个视图启动一个模态对话框,我需要它们之间的通信.我们正在使用 JQuery.

我有一个带有数据网格的名为 Charges.cshtml 的视图.数据网格的第一列可能有 span 元素或 link 元素,具体取决于属性将说明该费用是具有单个还是多个描述.视图如下所示.

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

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

这里的难点在于如何在两个视图之间传递数据.我可以通过控制器或通过 javascript 传递数据.

我尝试了各种方法将 Charges.cshtml 中的选定费用传递到 LoanCharge 控制器中的 LoadLoanChargeDescriptions 方法,如 json 序列化、ViewData、ViewBag、TempData 等,但没有用.我可以传递简单的数据类型,如 int、string、float,但不能传递整个对象.我觉得我需要将 CurrentDescription 和 Descriptions 传递给我的控制器,然后我需要将它们转移到其他部分.我试图传递字符串列表,但无法看到如何在控制器中访问它们,因为我在控制器中被计数为 0.我可以打开多个描述 UI 的弹出窗口(现在只添加了 Hello 文本)

请参阅下面的代码片段

费用.cshtml

@model ChargeViewModel@using (Html.FAFBeginForm()){<div><表格><tr >//.....<td>@if(Model.IsMultipleMatch){var creditCharge = Model as ChargeViewModel;如果(loanCharge.IsMultipleMatch == true){//string vm = @Newtonsoft.Json.JsonConvert.SerializeObject(loanCharge);<跨度>@loanCharge.Description</a></span>}}别的{<span>模型.描述</span>}</td></tr></tbody>

}公共类 ChargeViewModel{公共字符串 描述 {get;set;}public bool IsMultipleMatch {get;set;}公共列表<字符串>说明 {get;set;}}公共类 LoanChargeController{公共 ActionResult LoadLoanChargeDescriptions(){//在这里获取数据并传递/继续工作return View("_PartialMultipleMatchPopup", null);}}

在 Review.js 中

function ShowMatchingDescriptions(popUpURL, windowProperties, w, h) {尝试 {var left = (screen.width/2) - (w/2);var top = (screen.height/2) - (h/2);var 属性 = windowProperties + "dialogwidth:" + w + "px;dialogheight:" + h + "px;dialogtop:" + top + "px;dialogleft:" + left + "px;scroll:yes;resizable:no;center:yes;title:匹配的贷款人费用;";$.when(window.showModalDialog(popUpURL,窗口,属性)).then(函数(结果){var childWindow = 结果;});}抓住(错误){alert("错误:" + 错误)}}

更新 1

我更新了我的问题并发布了更多详细信息.

提前致谢.

更新 2

请在下面的链接中查看我的解决方案.

父子窗口之间的MVC传递模型

解决方案

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

 function ChargeViewModel() {this.Description ='';this.IsMultipleMatch =false;}var chargeViewModel=new ChargeViewModel();var data = JSON.stringify({'chargeViewModel':chargeViewModel });$.ajax({contentType: '应用程序/json;字符集=utf-8',数据类型:'html',类型:'POST',url: '@Url.Action("LoadLoanChargeDescriptions", "LoanChargeController")',数据:数据,成功:功能(结果){//结果将是您的部分页面html输出},失败:功能(响应){}});

然后你必须像这样改变控制器:

public ActionResult LoadLoanChargeDescriptions(ChargeViewModel chargeViewModel){//在这里获取数据并传递/继续工作return View("_PartialMultipleMatchPopup", null);}

让我知道您有疑问..

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.

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

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

解决方案

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..

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

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