Html.RenderAction使用POST而不是GET的 [英] Html.RenderAction uses Post instead of Get

查看:477
本文介绍了Html.RenderAction使用POST而不是GET的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网页上的简单形式。提交时,它会检查是否 ModelState.IsValid 并返回,如果它不是有效的同一型号的视图。

I have a simple form on my page. When submitted, it checks if ModelState.IsValid and returns the View with the same model if it's not valid.

在同一页上,我呈现包含另一种形式,像这样的动作:

On the same page, I'm rendering an action that contains another form like so:

Html.RenderAction("AccountNote", new { id = Model.ID });

直到我提出我的网页上表单和验证失败时,一切工作正常。当它再次显示该页面时,AccountNote动作的帖子事件触发时,我所期望的获取事件触发。我想这是有道理的,为什么它的发生,因为它的职位,该行动的渲染视图,但我想在GET事件,而不是火。

Everything works fine until I submit the form on my page and the validation fails. When it shows the page again, the AccountNote action's Post event fires when I'd expect the Get event to fire. I guess it makes sense why it's happening since it's the post that action that's rendering the view, but I want the Get event to fire instead.

public ActionResult AccountNote(int id)
{
    //code goes here...

     return PartialView(model);
}

[HttpPost]
public ActionResult AccountNote(AccountNoteViewModel model)
{
    //code goes here...

    return PartialView(model);
}

我做得不正确的?或者是有一些挂羊头卖狗肉我必须做的,使这项工作?我期望Html.RenderAction始终假定GET而不是POST。

Am I doing something incorrect? Or is there some trickery I have to do to make this work? I would expect the Html.RenderAction to always assume GET instead of POST.

推荐答案

一个解决办法是只生一个 AccountNote()操作方法。然后,它会被考虑的GET或POST的调用。您可能需要修改你的逻辑有点如果你使用 AccountNote的POST版本()

One solution would be to have only one AccountNote() action method. Then it will be called regardless of GET or POST. You might have to modify your logic a bit if you were using POST version of AccountNote().

你还可以用<一个装饰它href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.childactiononlyattribute%28v=vs.108%29.aspx\"相对=nofollow> [ChildActionOnly] 属性。

这篇关于Html.RenderAction使用POST而不是GET的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆