【问题标题】:Question about ASP.NET MVC & a form post using HTTP-POST关于 ASP.NET MVC 和使用 HTTP-POST 的表单发布的问题
【发布时间】:2009-04-05 02:26:36
【问题描述】:

我有一个标准的 ASP.NET MVC 表单帖子。

例如。

<% using (Html.BeginForm<CommentController>(c => c.Create())) { %>    
..
<% } %>

(and in the controller)

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Comment comment)
{ .. }

现在,我如何才能忽略某些值,例如IDCreatedOn 属性,它们可能存在于Comment 对象中?

有什么方法可以定义排除/黑名单

【问题讨论】:

    标签: asp.net-mvc controller


    【解决方案1】:

    使用带有排除标记的BindAttribute

    public ActionResult Create( [Bind(Exclude="ID, CreatedOn")]Comment comment )
    {
    }
    

    【讨论】:

    • Awesome Tvanfosson :) 我还假设 Include 属性是有效的,用于白名单?
    • 奥普斯。苏兹。 Tvanfosson - 我错过了你的 MSDN 链接。成员页面 (msdn.microsoft.com/en-us/library/…) 列出了 4 个属性。甜的。 Include 是其中之一,当然 Prefix 也非常方便。干杯队友!
    【解决方案2】:

    默认情况下,您在表单 HTML 中的任何输入都将与提交一起传递。

    很抱歉,我一时想不出答案,但我会先研究一些可能的 jQuery 插件和/或一些 AJAX 过滤机制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多