使用TempData在MVC中显示消息 [英] Show a Message in MVC using TempData

查看:101
本文介绍了使用TempData在MVC中显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在asp.net mvc中显示一条消息.为此,我创建了局部视图.此局部视图的名称为_feedback.在此局部视图的主体中,我编写了此代码.

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.

@model MyProject.SharedTools.OperationStatus

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

我将此代码放在_layout文件中:

i put this code in _layout file:

@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:

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

推荐答案

请确保您已通过您的零件所期望的正确模型:

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

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

如果您未将模型指定为 Html.Partial 帮助器的第二个参数,则它将自动传递当前视图的模型(在您的情况下,其类型为 MyProject.Models.ProvinceModel ),这就是为什么会出现错误:您的部分用户期望使用类型为 MyProject.SharedTools.OperationStatus 的模型.

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.

也不清楚在哪里使用存储在控制器内部的TempData值.也许应该是这样的:

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:

@model MyProject.SharedTools.OperationStatus

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

还是不是只想不使用模型就直接显示存储在TempData中的值?

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

@TempData["OperationStatus"]

这篇关于使用TempData在MVC中显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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