Javascript 在 ASP.NET MVC 中:从 Razor 视图调用控制器操作方法的所有可能方法

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

In ASP.NET MVC: All possible ways to call Controller Action Method from a Razor View

javascriptjqueryajaxasp.net-mvcrazor

提问by dan

I know this is a pretty basic question over here.

我知道这是一个非常基本的问题。

But could you tell me all possible optionsavailable to,
call a Control Action Method [generally any server side routine] from a Razor Viewand,
in what scenarios each are best applicableto be used in.

但是,您能否告诉我所有可用的选项从 Razor 视图
调用控制操作方法 [通常是任何服务器端例程],以及 每种方法最适用于
哪些场景

Thanks.

谢谢。

回答by Sampath

Method 1 :Using jQuery Ajax Get call (partial page update).

方法一:使用jQuery Ajax Get调用(部分页面更新)。

Suitable for when you need to retrieve jSon data from database.

适用于需要从数据库中检索 json 数据的情况。

Controller's Action Method

控制器的动作方法

[HttpGet]
public ActionResult Foo(string id)
{
    var person = Something.GetPersonByID(id);
    return Json(person, JsonRequestBehavior.AllowGet);
}

Jquery GET

查询获取

function getPerson(id) {
    $.ajax({
        url: '@Url.Action("Foo", "SomeController")',
        type: 'GET',
        dataType: 'json',
        // we set cache: false because GET requests are often cached by browsers
        // IE is particularly aggressive in that respect
        cache: false,
        data: { id: id },
        success: function(person) {
            $('#FirstName').val(person.FirstName);
            $('#LastName').val(person.LastName);
        }
    });
}

Person class

人物类

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Method 2 :Using jQuery Ajax Post call (partial page update).

方法二:使用 jQuery Ajax Post 调用(部分页面更新)。

Suitable for when you need to do partial page post data into database.

适用于需要将部分页面发布数据到数据库中时。

Post method is also same like above just replace [HttpPost]on Action method and type as postfor jquery method.

Post 方法也和上面一样,只是替换 [HttpPost]Action 方法并输入postjquery 方法。

For more information check Posting JSON Data to MVC Controllers Here

有关更多信息,请在此处查看将JSON 数据发布到 MVC 控制器

Method 3 :As a Form post scenario (full page update).

方法 3:作为表单发布场景(整页更新)。

Suitable for when you need to save or update data into database.

适用于需要将数据保存或更新到数据库中的时候。

View

看法

@using (Html.BeginForm("SaveData","ControllerName", FormMethod.Post))
{        
    @Html.TextBoxFor(model => m.Text)

    <input type="submit" value="Save" />
}

Action Method

动作方法

[HttpPost]
public ActionResult SaveData(FormCollection form)
    {
        // Get movie to update
        return View();
   }

Method 4 :As a Form Get scenario (full page update).

方法 4:作为 Form Get 方案(整页更新)。

Suitable for when you need to Get data from database

适用于需要从数据库中获取数据的时候

Get method also same like above just replace [HttpGet]on Action method and FormMethod.Getfor View's form method.

Get 方法也和上面一样,只是替换 [HttpGet]了 Action 方法和FormMethod.GetView 的 form 方法。

I hope this will help to you.

我希望这会对你有所帮助。