开发者

Request.Forms.Keys.Count = 0 on Postback

开发者 https://www.devze.com 2023-02-10 15:24 出处:网络
I have the following HTML on my displayed form <fieldset> <legend>Edit User Roles</legend>

I have the following HTML on my displayed form

<fieldset>
<legend>Edit User Roles</legend>
<ul>
    <% foreach (string role in (string[]) ViewData["roles"]){ %>
        <li>
            <div id="Div4" class="grid_6">
                <div id="Div5" class="grid_2 alpha" style="font-weight: bold;">
                    <%= Html.CheckBox("role." + role,  Roles.IsUserInRol开发者_JS百科e(Model.UserName, role))%>
                </div>
                <div id="Div6" class="grid_3 omega" style="font-family: Verdana; font-size: 10pt;">
                    <label for="role.<%: role %>">
                        <%: role %></label><br />
                </div>
            </div>
        </li>
    <% } %>
</ul>
</fieldset>

I have the following code in my Controller

        [HttpPost]
    public ActionResult EditUser( string id, bool approved )
    {
        int i = Request.Form.Keys.Count 

        foreach (string key in Request.Form.Keys)
        {
            if (key.StartsWith( "role." ))
            {
                // Do something
            }
        }

        MembershipUser membershipUser = Membership.GetUser( id );

        return View( membershipUser );
    }

If I break the code and explore, I find that the Request.Form.Keys.Count = 0, although there should be at least 4 keys created with "role." as a prefix from four checkboxes displayed on the form.

What am I not understanding here?


Request.Form.Keys.Count = 0 could have two possible explanations:

  • No value was sent in the POST body
  • You used some special content type such as for example application/json instead of application/x-www-form-urlencoded (could happen if you play with AJAX)

I would recommend you using FireBug to see exactly what's contained in the POST request and if there are any values. You haven't shown the form definition neither how you are submitting it. If you are POSTing with AJAX maybe there's where the problem lies.

Here's an example of how a valid request might look like in FireBug:

Request.Forms.Keys.Count = 0 on Postback

0

精彩评论

暂无评论...
验证码 换一张
取 消