从单独的控制器(ASP.NET MVC3)调用
我正在尝试使用操作链接在单独的控制器中调用操作。
问题是 [HttpGet] 和 [HttpPost] 操作都会被调用,并且由于 post 方法返回调用该操作的视图,因此不会显示任何内容。
获取方法:
[HttpGet]
public ActionResult View(int id, int index)
{
var form = formService.GetForm(id);
var pageModel = new PageViewModel();
var page = form.Pages.ElementAt(index);
ModelCopier.CopyModel(page, pageModel);
ModelCopier.CopyModel(form, pageModel);
return View(pageModel);
}
发布方法
[HttpPost]
public ActionResult View(PageViewModel pageViewModel)
{
if (!ModelState.IsValid)
{
return RedirectToAction("Details", "Forms", new { id = pageViewModel.FormId });
}
var pageToEdit = pageService.GetPage(pageViewModel.PageId);
ModelCopier.CopyModel(pageViewModel, pageToEdit);
pageService.SavePage();
return RedirectToAction("Details", "Forms", new {id = pageViewModel.FormId});
}
如何从视图调用它(从另一个控制器返回的视图):
@Html.ActionLink("View", "View", "Pages", new { id = Model.FormId, index = item.Index-1 }, null)
我在这里做错了什么?我本质上希望它作为更新/编辑功能。返回的视图包含视图模型的简单表单。
I'm trying to call an action in a separate controller with an actionlink.
The problem is that both the [HttpGet] and the [HttpPost] action gets called, and since the post-method returns the view from which the action is called, nothing gets displayed.
Get method:
[HttpGet]
public ActionResult View(int id, int index)
{
var form = formService.GetForm(id);
var pageModel = new PageViewModel();
var page = form.Pages.ElementAt(index);
ModelCopier.CopyModel(page, pageModel);
ModelCopier.CopyModel(form, pageModel);
return View(pageModel);
}
Post Method
[HttpPost]
public ActionResult View(PageViewModel pageViewModel)
{
if (!ModelState.IsValid)
{
return RedirectToAction("Details", "Forms", new { id = pageViewModel.FormId });
}
var pageToEdit = pageService.GetPage(pageViewModel.PageId);
ModelCopier.CopyModel(pageViewModel, pageToEdit);
pageService.SavePage();
return RedirectToAction("Details", "Forms", new {id = pageViewModel.FormId});
}
How it's called from the View (from a view returned by another controller):
@Html.ActionLink("View", "View", "Pages", new { id = Model.FormId, index = item.Index-1 }, null)
What am I doing wrong here? I essentially want it to work as an update/edit function. And the view returned contains a simple form for the viewmodel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
操作链接发出 GET 请求。您必须实现一些 JavaScript 函数来捕获 url 和参数,并使用 POST 方法动态创建和提交新表单,或者执行 Ajax POST。您可以编写自己的 HTML Helper 来包装此功能,但默认功能是单击
标记(这是由
Html.ActionLink
生成的) ) 将发出 GET 请求。An action link issues a GET request. You will have to implement some JavaScript function that captures the url and parameters, and dynamically creates and submits a new form with a POST method, or does an Ajax POST. You could write your own HTML Helper, to wrap this functionality, but the default functionality of clicking an
<a>
tag (which is what is generated by aHtml.ActionLink
) will issue a GET request.我认为您无法使用操作链接 - 请参阅此 问题
选项是使用 jQuery 发布数据或交换到简单的表单并提交
I don't think you'll be able to use an action link - see this question
The options are to use jQuery to post data or swap to a simple form and submit