C#:从视图文件夹调用控制器方法
问题描述:
我完全是MVC框架的noob。我想打电话给Controller Controller文件夹中的Agentcontroller的操作方法。C#:从视图文件夹调用控制器方法
我试图从查看文件夹叫
Controller/Agentcontroller/myactionmethod()
View/Agent/CodeGenerate.cshtml
我觉得我在与路线的麻烦,但我不能找到App_Start文件夹。这是我正在使用的脚本。提前MVC - calling controller from view
感谢:
<script type="text/javascript">
function G() {
$.ajax({
type: "post",
url: '/AgentsControllers/',
data: $('form').serialize(),
success: function (response) {
alert("Hi");
}
});
}
</script>
我也检查了这一点,以及!
答
你的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> Save</button>
}
+0
感谢哥们!我现在将测试它。 –
+0
请检查并让我知道如果您需要任何东西,否则请接受此答案。 –
这个班级没有这样的“路径”,它有一个路线 - 你能发布你如何设置你的路线吗? – Alex
糟糕。对于那个很抱歉。但是我找不到App_Start文件夹。 –
同意@alex你需要看看你的路线是如何设置的。通常你不会使用URL'/ AgentsControllers',它很可能只是'/ Agent',然后根据你的路由设置如何调用控制器上的特定方法/动作。 –