asp.net-mvc 如何从控制器传递字符串消息以在 MVC 中查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23477560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:40:45  来源:igfitidea点击:

How to pass a string message from controller to view in MVC

asp.net-mvcasp.net-mvc-4

提问by user3172140

I want to display a string message through a string variable by passing from controller to View.

我想通过从控制器传递到视图来通过字符串变量显示字符串消息。

Here is my controller code:

这是我的控制器代码:

public ActionResult SearchProduct(string SearchString)
        {

            FlipcartDBContextEntities db = new FlipcartDBContextEntities();
            string noResult="Search Result Not Found";
            var products = from p in db.Products  select p;

            if (!String.IsNullOrEmpty(SearchString))
            {
                products = products.Where(s => s.ProductName.StartsWith(SearchString));
                return View(products);

            }

            else
            {

                return View(noResult);
            }

// Here i want to display the string value ei Message to the view.

// 这里我想向视图显示字符串值 ei Message。

please guide me. Am new to MVC

请指导我。我是 MVC 的新手

回答by Brian Mains

Change your controller to:

将您的控制器更改为:

    public ActionResult SearchProduct(string SearchString)
    {

        FlipcartDBContextEntities db = new FlipcartDBContextEntities();
        string noResult="Search Result Not Found";
        var products = from p in db.Products  select p;

        if (!String.IsNullOrEmpty(SearchString))
        {
            products = products.Where(s => s.ProductName.StartsWith(SearchString));
            return View(products.ToList());

        }

        else
        {
            ViewBag.Message = noResult;
            return View(new List,Product>());
        }

You can pass the message from the server to the client via the ViewBag. Note that you have to return the same API from both sides of the if/else, so you can't pass a list of products one time and a string the other. In your view:

您可以通过 ViewBag 将消息从服​​务器传递到客户端。请注意,您必须从 if/else 的两侧返回相同的 API,因此您不能一次传递产品列表,而另一次传递一个字符串。在您看来:

if (ViewBag.Message != null)
{
   <span>@ViewBag.Message</span>
}

Or don't do any of that and just put the message in the view based on the existence of a product list having items within the list.

或者不要做任何事情,只是根据列表中包含项目的产品列表的存在将消息放入视图中。

// Model = Products returned; must make sure list returned is not null
if (Model.Count > 0)
{
   <span>Search Result not found</span>
}

Or even as another option you can create a model class:

或者甚至作为另一种选择,您可以创建一个模型类:

public class SearchModel
{
   public List<Product> Products { get; set; }

   public string EmptyMessage { get; set; }
}

And return this via your view method:

并通过您的视图方法返回它:

//if
return View(new SearchModel { Products = products });
//else
return View(new SearchModel { EmptyMessage = "Search result Not Found" });