如何在mvc中的同一视图中返回一个字符串

时间:2015-09-23 14:48:17

标签: asp.net-mvc

我正在尝试从控制器返回一个字符串到MVC中的相同视图

但它返回到新视图

以下是我的观点代码:

@using (Html.BeginForm("Index2", "home", FormMethod.Post))
{

<label></label>    
 <input type="submit" value="Click Me" />
}

以下是我的控制器的代码:

  public ActionResult Index2()
    {
        string _return = "Hello world";
        return Content(_return);

    }

如何在标签

中显示“hello world”

感谢.....

2 个答案:

答案 0 :(得分:1)

制作视图的模型类型string,如下所示:

@model string

在代码中使用该字符串,如下所示:

The action told me to say @Model

另一种方法是,如果要显示操作方法的内容,请在视图中调用Html.Action

@Html.Action("Index2")

答案 1 :(得分:1)

You can pass the value using Model objects or simply use ViewBag

public ActionResult Index2()
{
    ViewBag.return = "Hello world";
    return View("YourViewName");

}
  

在视图中

<label>@ViewBag.return</label>