【发布时间】: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