asp.net-mvc 从 view-MVC .NET 调用非操作方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18883923/
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:10:01  来源:igfitidea点击:

Calling non-Action methods from view-MVC .NET

asp.net-mvcasp.net-mvc-4

提问by rsj

I want to call a non-action method in a view. Can I do that? In other words, there is a method in my controller which returns a string. I want to call that method in my view (.cshtml) and show it in the view. All I could see was, the methods should return an ActionResult. But in my case I do not need a view returned just a string.

我想在视图中调用非操作方法。我可以这样做吗?换句话说,我的控制器中有一个返回字符串的方法。我想在我的视图 (.cshtml) 中调用该方法并在视图中显示它。我只能看到,这些方法应该返回一个 ActionResult。但在我的情况下,我不需要只返回一个字符串的视图。

回答by Matt Millican

The best way (and MVC convention way) to do this is to use a ViewModel. Quick example:

执行此操作的最佳方法(和 MVC 约定方法)是使用ViewModel. 快速示例:

Model

模型

public class MyViewModel {
    public string StringValue { get; set; }
}

Controller

控制器

public class MyController : Controller 
{
    public ActionResult MyView() 
    {
        var model = new MyViewModel
        {
            StringValue = CallMyMethod()
        }

        return View(model);
    }
}

View

看法

@model MyViewModel @*(use fully qualified namespace)*@

@Model.StringValue @*<-- this will print it out on the view*@

Hope that helps!

希望有帮助!

回答by scartag

You can do that if you want.

如果你愿意,你可以这样做。

Although i wouldn't call it good practice.

虽然我不会称之为好的做法。

You can call whatever methods you want on the view but you should try to keep you views as clean as possible and do the barest minimum of logic you require.

你可以在视图上调用任何你想要的方法,但你应该尽量让你的视图保持干净,并做你需要的最低限度的逻辑。

In your controller you could have this method below.

在您的控制器中,您可以在下面使用此方法。

public string GetName(string name)
{
   return string.Format("<strong>{0}</strong>", name);
}

And in your view you could use it as below.

在您看来,您可以按如下方式使用它。

@{var c = new Controller();}

@c.GetName("My Name") //some other place in your view

回答by Erik Philips

You can return View/Partial view to other views by calling Html.Action() or Html.RenderAction()

您可以通过调用Html.Action() 或 Html.RenderAction()将 View/Partial 视图返回到其他视图

I've personally never had the need to return a value from a method that wasn't a base class of ActionResult, as there are quite a few.

我个人从来没有需要从不是 的基类的方法中返回一个值ActionResult因为有很多.

回答by Jestin

This sounds like a good use case for a custom Helper.

这听起来像是自定义 Helper 的一个很好的用例。

http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

回答by axy108

You can return a Json object from the controller with something like this:

您可以使用以下内容从控制器返回一个 Json 对象:

[HttpPost]
public ActionResult YourFunction()
{
    return Json(new { stringContent = "Your String content here!" });
}

And then you can do an ajax call from your .cshtml like this:

然后你可以像这样从你的 .cshtml 做一个 ajax 调用:

$.ajax(
{
    url: '/YourController/YourFunction',
    type: 'POST',
    success: function (data) {
                $("#SomeElement").html(data.stringContent);
             },
             error: function () {
                 alert("error");
             }
});

Calling methods from your View that are in your Controller is not recommended as you are breaking the MVC pattern!

不建议从控制器中调用视图中的方法,因为您正在破坏 MVC 模式!