asp.net-mvc – HttpPost和HttpGet属性在MVC:为什么使用HttpPos
发布时间:2020-12-15 19:19:57 所属栏目:asp.Net 来源:网络整理
导读:所以我们有[HttpPost],这是一个可选属性。我理解这限制了调用,所以它只能通过HTTP POST请求。我的问题是为什么我想这样做? 解决方法 想象以下: [HttpGet]public ActionResult Edit(int id) { ... }[HttpPost]public ActionResult Edit(MyEditViewModel m
所以我们有[HttpPost],这是一个可选属性。我理解这限制了调用,所以它只能通过HTTP POST请求。我的问题是为什么我想这样做?
解决方法
想象以下:
[HttpGet] public ActionResult Edit(int id) { ... } [HttpPost] public ActionResult Edit(MyEditViewModel myEditViewModel) { ... } 这是不可能的,除非ActionMethodSelectorAttributes HttpGet和HttpPost使用。 我将是大胆的,并说这是最好的做法,当涉及到CRUDish的东西在ASP.NET MVC。 编辑: @TheLight问在视图中需要什么来完成这个职位。它只是一个带有方法POST的表单。 使用Razor,这看起来像这样。 @using (Html.BeginForm()) { <input type="text" placeholder="Enter email" name="email" /> <input type="submit" value="Sign Up" /> } 这将呈现以下HTML: <form action="/MyController/Edit" method="post"> <input type="text" name="email" placeholder="Enter email"> <input type="submit" value="Sign Up"> </form> 当表单提交时,它将向控制器执行Http Post请求。具有HttpPost属性的操作将处理请求。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 获取Telerik RadScheduler当前视图的日期范围
- 如何处理ASP.NET Core中HTML5客户端路由回退的问题
- asp.net-mvc – 无法在mvc 4中映射特定控制器的路由
- asp-classic – 经典ASP速成课程
- asp.net-mvc – 如何仅刷新MVC 5中的部分索引页面?
- 用于创建ASP.NET成员资格提供程序用户的脚本
- asp.net-mvc – MVC 4中的主 – 详细信息样式视图
- asp.net-mvc – 如何为每个ActionLink指定默认区域而不添加
- asp.net-mvc-3 – 绑定到MVC3中的DropDownList
- asp.net-mvc – 如何每秒自动刷新MVC PartialView
推荐文章
站长推荐
- asp.net – 如何在使用edmx文件时在连接字符串中
- asp.net mvc更新多条记录
- asp.net-mvc – 如何下载Razor View引擎
- asp.net-mvc – MVC在导航栏中显示登录用户的名称
- 更改生成的ASP.Net ID?
- asp.net – Internet Explorer 8超时太快页面POS
- asp.net – 如何为客户端和服务器缓存设置不同的
- asp.net-mvc – ASP.NET MVC中的视图文件/目录结
- asp.net – 相当于Sitecore中的Razor Section He
- asp.net-web-api – 在Azure Service Fabric中,无
热点阅读