【发布时间】:2014-01-06 23:10:23
【问题描述】:
在视图中如何调用控制器中的方法或项目中的另一个类?我可以这样做吗?
【问题讨论】:
标签: asp.net asp.net-mvc-4
在视图中如何调用控制器中的方法或项目中的另一个类?我可以这样做吗?
【问题讨论】:
标签: asp.net asp.net-mvc-4
如果您需要在视图中调用操作方法,则需要使用ChildActionOnly:
[ChildActionOnly]
public ActionResult action1()
{
//
return PartialView();
}
那么在你看来:
@Html.Partial("action1")
但是如果你想调用一个像帮助类这样的方法,你可以这样:
@helper ShowTree()
{
//some code
}
【讨论】:
试试这个,
下面提到如何查看调用控制器动作事件的所有格式。
-> 使用 Ajax beginform 调用
@using (Ajax.BeginForm("ContainerSizeDetail", "Content", FormMethod.Post, null, new { @class = "" }))
{}
[HttpPost]
public ActionResult ContainerSizeDetail(ContainerSizeModel model)
{}
-> 使用 Beginform 调用
@using (Html.BeginForm("VendorContactDetail", "VendorAccount", FormMethod.Post, new { id = "frmVendorContact" }))
{}
[AllowAnonymous]
public ActionResult VendorContactDetail()
{}
-> 使用 Ajax 调用
var request = $.ajax({
url: "http://localhost/ProjectDirectory/VendorAccount/ValidateUser",
type: 'POST',
cache: false,
data: JSON.stringify(returnValue),
dataType: 'json',
contentType: 'application/json; charset=utf-8'
});
[AllowAnonymous]
[HttpPost]
public ActionResult ValidateUser(VendorRegistrationModel model)
{}
【讨论】: