【问题标题】:Flip the model instance through controller return to view通过控制器翻转模型实例返回视图
【发布时间】:2020-02-06 14:59:57
【问题描述】:
  • 我创建了一个模型,我在控制器中创建了模型的实例

  • 我想将此实例数据发送到部分视图(不起作用)

型号

 public partial class userDaty_Model
    {
        public int liczbaDniM { get; set; }

        public int numerMiesiacaM { get; set; }

        public int numerRokuM { get; set; }
    }



// which is part of ParentView


public partial class ParentView
    {
        public userDaty_Model Model4 { get; set; }
    }

我从中调用 partialView 的主视图中的 Ajax 代码。

 var userDate = {
             numerMiesiaca: $("#id_sb_month").dxSelectBox("instance").option("value"),
             numerRoku: $("#id_sb_year").dxSelectBox("instance").option("value"),
             liczbaDni: liczbaDni
         };


        $.ajax({

            url: "@Url.Action("PartialTabelaEcp", "Home")",
            type: "POST",
            dataType: "json",
            data: {"userDate": JSON.stringify(userDate)},
            cache: false,
            success: function (data) {
                $("#kartaEcp").html(data);
            },
            failure: function (error) {
                alert(error);

            },
            error: function (error) {
                alert(error);
            }
        });

控制器:

 [HttpPost]
        public ActionResult PartialTabelaEcp(string userDate)
        {
int liczbaDni =2;
int numerMiesiaca = 6;
int numerRoku = 12;

userDaty_Model userDaty = new userDaty_Model();
            userDaty.liczbaDniM = liczbaDni;
            userDaty.numerRokuM = numerMiesiaca;
            userDaty.numerMiesiacaM = numerRoku;


// return, which returns the view
 return PartialView(@"~/Views/Home/_TabelaEwidencja.cshtml");

// return who I want to return but gives me nothing
 return PartialView(@"~/Views/Home/_TabelaEwidencja.cshtml", userDaty );
}


***edit
 I changed and I have "return PartialView("_TabelaEwidencja", userDaty);"and it is still the same as it was

我将红点设置为返回,当没有'userDate'模型的实例时,它会通过返回

当它在 错误

jquery.js:9837 POST https://localhost:44362/Home/PartialTabelaEcp 500(内部服务器错误)

之后: 错误

加载资源失败:服务器响应状态为 500(内部服务器错误)

部分视图

@model ParentView


// ...

有谁知道我做错了什么?

【问题讨论】:

  • 500 错误表示在服务器端抛出了错误(在您的 PartialTabelaEcp 控制器操作中)。用 try/catch 块包围控制器操作内的代码,并在 catch 部分设置断点。运行您的代码,找出异常所在,然后自行修复或在此处报告,我们将为您提供更多帮助。此外,您在操作中返回的模型看起来与局部视图中的预期模型不匹配,您不需要指定局部视图的整个路径
  • @GregH 我更新了,我将红点设置为返回,当没有“userDate”模型的实例时,它会通过返回,当它处于错误 jquery.js:9837 POST localhost:44362/Home/PartialTabelaEcp 500(内部服务器错误)
  • 那么听起来你的错误是在剃刀视图的编译过程中,这表明你将错误的模型传递给你的局部视图或者你的局部视图有其他问题。你应该能够在你的控制器中覆盖OnException(有很多关于如何做到这一点的例子)并在那里设置一个断点来找到实际的错误。你给了我来自客户端的错误消息,这不是你需要找到的。你需要找到它在哪里失败的服务器端

标签: c# asp.net asp.net-mvc asp.net-core model-view-controller


【解决方案1】:

如下改变:

1.Ajax(从dataType:"json"更改为dataType:"html"):

$.ajax({
    url: "@Url.Action("PartialTabelaEcp", "Home")",
    type: "POST",
    dataType: "html",   
    data: {"userDate": JSON.stringify(userDate)},
    cache: false,
    success: function (data) {
        $("#kartaEcp").html(data);
    }
});

2.控制器:

[HttpPost]
public ActionResult PartialTabelaEcp(string userDate)
{
    int liczbaDni = 2;
    int numerMiesiaca = 6;
    int numerRoku = 12;

    userDaty_Model userDaty = new userDaty_Model();
    userDaty.liczbaDniM = liczbaDni;
    userDaty.numerRokuM = numerMiesiaca;
    userDaty.numerMiesiacaM = numerRoku;

    var data = new ParentView()
    {
        Model4 = userDaty
    };
    return PartialView("~/Views/Home/_TabelaEwidencja.cshtml", data);
}

【讨论】:

    【解决方案2】:

    你应该在你的@model 中使用正确的模型,如果你给他 Daty 模型但是告诉他它是一个父视图模型它会抛出一个错误

    【讨论】:

      【解决方案3】:

      这是因为你不能返回视图的完整路径,视图总是相对于 Views 文件夹内的文件夹和控制器的名称(当然可以更改),例如:

      /Views/Home
      /Views/Admin
      

      所以,你应该改为调用:

      return PartialView("_TabelaEwidencja", userDaty);
      

      另外,在您看来,声明正确的模型类型:userDaty_Modeldynamic,这是使用 @model 关键字。

      【讨论】:

      • 我改了,我有"return PartialView("_TabelaEwidencja", userDaty);"还是和原来一样
      猜你喜欢
      • 2013-03-22
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多