使用TempData在MVC中显示消息

时间:2022-09-26 19:08:28

i want to show a message in asp.net mvc. for this, i create a partial view. name of this partial view is _feedback. in body of this partial view i write this codes.

我想在asp.net mvc中显示一条消息。为此,我创建了一个局部视图。此部分视图的名称是_feedback。在这个局部视图的正文中,我写了这些代码。

@model MyProject.SharedTools.OperationStatus

@if (Model != null)
{
    if (Model.IsSuccess)
    {
        @:Model.Message;
    }
    else
    {
        @:Model.Message;
    }
}

i put this code in _layout file:

我把这段代码放在_layout文件中:

@Html.Partial("_feedback")

and when i want to see a message from controller, using this code:

当我想从控制器看到一条消息时,使用以下代码:

 operationStatus = _provinceRepository.Save();
 if (operationStatus.IsSuccess)
 {
     TempData["OperationStatus"] = operationStatus;
     return RedirectToAction("Index");
 }

but i give this error:

但我给出了这个错误:

The model item passed into the dictionary is of type 'MyProject.Models.ProvinceModel', but this dictionary requires a model item of type 'MyProject.SharedTools.OperationStatus'.

传递到字典中的模型项的类型为“MyProject.Models.ProvinceModel”,但此字典需要“MyProject.SharedTools.OperationStatus”类型的模型项。

1 个解决方案

#1


1  

Make sure that you have passed the correct model that your partial is expecting:

确保您已经传递了部分期望的正确模型:

@Html.Partial("_feedback", Model.SomePropertyOfTypeOperationStatus)

If you do not specify a model as second argument to the Html.Partial helper, then it will automatically pass the model of the current view (which in your case is of type MyProject.Models.ProvinceModel) and that's why you are getting the error : your partial expects a model of type MyProject.SharedTools.OperationStatus.

如果你没有指定模型作为Html.Partial助手的第二个参数,那么它将自动传递当前视图的模型(在你的情况下是MyProject.Models.ProvinceModel类型),这就是你得到错误的原因:你的部分期望MyProject.SharedTools.OperationStatus类型的模型。

Also it is not quite clear where you are using the TempData value that you stored in your controller inside your partial. Maybe it should be something like this:

此外,您在部分中使用存储在控制器中的TempData值的位置也不太清楚。也许它应该是这样的:

@model MyProject.SharedTools.OperationStatus

@if (Model != null)
{
    @TempData["OperationStatus"]
}

or didn't you just mean to display directly the value you stored in TempData in your partial without using a model?

或者你不是只想在不使用模型的情况下直接显示存储在部分TempData中的值?

@TempData["OperationStatus"]

#1


1  

Make sure that you have passed the correct model that your partial is expecting:

确保您已经传递了部分期望的正确模型:

@Html.Partial("_feedback", Model.SomePropertyOfTypeOperationStatus)

If you do not specify a model as second argument to the Html.Partial helper, then it will automatically pass the model of the current view (which in your case is of type MyProject.Models.ProvinceModel) and that's why you are getting the error : your partial expects a model of type MyProject.SharedTools.OperationStatus.

如果你没有指定模型作为Html.Partial助手的第二个参数,那么它将自动传递当前视图的模型(在你的情况下是MyProject.Models.ProvinceModel类型),这就是你得到错误的原因:你的部分期望MyProject.SharedTools.OperationStatus类型的模型。

Also it is not quite clear where you are using the TempData value that you stored in your controller inside your partial. Maybe it should be something like this:

此外,您在部分中使用存储在控制器中的TempData值的位置也不太清楚。也许它应该是这样的:

@model MyProject.SharedTools.OperationStatus

@if (Model != null)
{
    @TempData["OperationStatus"]
}

or didn't you just mean to display directly the value you stored in TempData in your partial without using a model?

或者你不是只想在不使用模型的情况下直接显示存储在部分TempData中的值?

@TempData["OperationStatus"]