2013-02-01 13 views
1

我有这种形式在我的asp网络mvc应用程序中,检查正确的形式发布的字段。他们中的一些人失踪了。不容明白为什么Asp net mvc表单不会发布所有提交的字段,因为它应该发布

<form id="FormRegistroUsuario" action="/Account/AdminSecurityUserAccountAdd"> 
    <fieldset> 
     <legend><h2 style="color:black;">Cadastro novo Usuário</h2></legend> 
     <table id="tblUsuario"> 
      <tr> 
       <td class="smallField">Username:</td> 
       <td>@Html.TextBoxFor(m => m.UserName,new { @class = "validate[required]" }) </td> 
       <td class="smallField" style="padding-left:10px;">Email:</td> 
       <td>@Html.TextBoxFor(m => m.Email, new { @class = "validate[required,custom[email]]" }) </td> 
       <td style="padding-left:10px;">Ativo:</td> 
       <td>@Html.CheckBoxFor(m => m.Active)</td> 
      </tr> 
      <tr> 
       <td class="smallField">Senha:</td> 
       <td>@Html.PasswordFor(m => m.Password, new { @class = "validate[required, minSize[7]]" }) </td> 
       <td style="padding-left:10px;">Repetir Senha:</td> 
       <td>@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "validate[required,equals[Password]]" }) </td> 
       <td style="font-size:10px; padding-left:10px;">Habilitar Login com Cert. Digital:</td> 
       <td>@Html.CheckBoxFor(m => m.isCertifiedAdd) 
        <div id="certDigitalBlockAdd"> 
         <table> 
          <tr> 
           <td class="smallField">Tipo do Certificado:</td> 
           <td>@Html.DropDownListFor(m => m.certTypeAdd, new SelectList(Model.certTypeComboBox, "id", "type"), "Escolha...", new { @class="smallField" })</td> 
           <td style="display:none;">@Html.Hidden("certTypeAddHidden")</td> 
          </tr> 
          <tr> 
           <td>Identificação:</td> 
           <td>@Html.TextBoxFor(m => m.identifierAdd, new { @class = "validate[required] smallField" })</td> 
           <td style="display:none;">@Html.Hidden("identifierAddHidden")</td> 
          </tr> 
         </table> 
        </div> 
       </td> 
      </tr> 
      <tr> 
       <td class="smallField">Pergunta Secreta:</td> 
       <td>@Html.TextBoxFor(m => m.SecretQuestion, new { @class = "validate[required]" }) </td> 
       <td style="padding-left:10px;">Resposta:</td> 
       <td>@Html.TextBoxFor(m => m.SecretQuestionPassword, new { @class = "validate[required, minSize[6]]" }) </td> 
       <td></td> 
       <td></td> 
      </tr>          
     </table> 
     <br /> 
     <div class="scrollTable" style="margin-left:20px;"> 
      <span class="boxTitle"><table><tr><td>Grupos Disponíveis</td></tr></table></span> 
      <!-- Tipos --> 
      <div class="scroller"> 
       <table> 
        <!-- Indices Disponíveis (Exibidos) --> 
        @{ 
        counter = 1; 
        } 
        @foreach (var role in Model.roles) 
        { 
        <tr id="@counter" class="whiteRow setIndex"> 
         <td class="adminTipoFormRowIndex">@role</td> 
        </tr> 
         counter++; 
        } 
       </table> 
      </div> 
     </div> 
     <div class="scrollTable" style="margin-left:60px;"> 
      <span class="boxTitle"><table><tr><td>Grupos Atribuidos</td></tr></table></span> 
      <!-- Tipos --> 
      <div class="scroller scrollerindex"> 
       <table> 
       @{ 
       counter = 1; 
       } 
       @foreach (var role in Model.roles) 
       { 
        string count = counter + "_a"; 
        string inputCount = counter + "_i"; 
        <tr id="@count" class="whiteRow unsetIndex" style="display:none;"> 
         <td class="adminTipoFormRowIndex">@role</td> 
         <input type="hidden" id="@inputCo**strong text**unt" name="rolesGroup" value="@role" disabled="disabled"/> 
        </tr> 
        counter++; 
       } 
       </table> 
      </div> 
     </div> 
     <input type="submit" value="Salvar" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button-link submitAddTipo"/>   
    </fieldset>   
</form> 

JS:

$('#FormRegistroUsuario').submit(function (e) { 
     e.preventDefault(); 
     var form = $(this); 
     console.log(form.serialize()); 
    }); 

字段被张贴:

如果我使用@ Ajax.beginForm或这种做法的结果是一样的
UserName=Guilherme 
Email=grlongo.ireland%40gmail.com 
Active=true 
Active=false 
Password=%23t1g2p3& 
ConfirmPassword=%23t1g2p3& 
isCertifiedAdd=true 
isCertifiedAdd=false 
SecretQuestion=Qual+a+senha%3F& 
SecretQuestionPassword=secreta 
rolesGroup=Administrador 

没关系。

  • 使用Ajax.beginForm
  • 发送和Ajax提交处理
  • 创建隐藏的额外的隐藏字段,以检查他们是否会公布 (真的)。

任何帮助你非常感激。

+0

您的隐藏字段没有价值,也许这就是为什么他们不包括在内。 –

+0

哈哈...男人,我还没有想过这件事。我来自巴西。感谢那。我现在改变! –

+0

这就是为什么一个人必须使用抽象作为工具,但了解内部运作。 –

回答

2

发现问题。隐藏字段在div的内部,得到display:none,因此值不会被发布。刚刚移动到我的表单的顶部,它现在正在工作。

2

当你在一个数组循环,您需要使用HTML相同的技术......

例如,在您的收藏Model.roles,你需要这样做:

@for (int counter = 0; counter <= Model.roles.Count(); counter++) 
{ 
    var role = Model.roles[counter]; 

    <input type="checkbox" 
      id="[email protected]" 
      name="roles[@counter]" 
      value="@role" @(role.active ?? "checked='checked'" : "" /> 
} 

代码简化删除

而且记得,任何空场从未张贴...

btw:您的表单中只有一个Active属性,但通过提交它写入其中的两个......您生成的HTML是什么?

+0

这是在asp net mvc中使用html助手复选框时的标准行为。值始终设置在始终具有元素[0]和元素[1]的元素的索引0中 –