从单独的控制器(ASP.NET MVC3)调用

发布于 2024-10-31 03:18:07 字数 1313 浏览 0 评论 0原文

我正在尝试使用操作链接在单独的控制器中调用操作。

问题是 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_畞蕅 2024-11-07 03:18:08

操作链接发出 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 a Html.ActionLink) will issue a GET request.

只是偏爱你 2024-11-07 03:18:08

我认为您无法使用操作链接 - 请参阅此 问题

选项是使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文