C#:从视图文件夹

时间:2017-06-14 09:46:26

标签: c# asp.net-mvc asp.net-ajax filepath

我完全是MVC框架的菜鸟。我想调用我在Agent文件夹中的AgentController动作方法。

我试图打电话

Controller/Agentcontroller/myactionmethod()
来自查看文件夹的

View/Agent/CodeGenerate.cshtml

我认为我遇到了路线问题但我无法找到App_Start文件夹。这是我正在使用的脚本。

<script type="text/javascript">

    function G() {      
        $.ajax({
            type: "post",
            url: '/AgentsControllers/',
            data: $('form').serialize(),
            success: function (response) {
                alert("Hi");
            }
        });
    }
</script>

我也检查了这一点:MVC - calling controller from view

提前致谢!

1 个答案:

答案 0 :(得分:1)

您的Ajax调用应该是:

<script type="text/javascript">

    function G() {      
        $.ajax({
            type: "post",
            url: '/AgentsControllers/myactionmethod/',
            data: $('form').serialize(),
            success: function (response) {
                alert("Hi");
            }
        });
    }
</script>

您的控制器操作方法应为:

[HttpPost]
public ActionResult myactionmethod(YourModelName objYourModelobject)
{

return PartialView("~/Views/Agent/CodeGenerate.cshtml", objYourModelobject);
}

您的观点应该是:

@model Application.Model.YourModel

@using (Html.BeginForm("Agentcontroller", "myactionmethod", FormMethod.Post, new { @class = "example" }))
{
    //HTML Helpers
     @Html.HiddenFor(model => model.Id, new { @id = "hdnDetailId" })
 <button type="submit" class="btn btn-success" id="btnSave"><i class="fa fa-floppy-o"></i>&nbsp;Save</button>
}