使用Html.运行时BeginForm

use Html.BeginForm at runtime

本文关键字:BeginForm 运行时 Html 使用      更新时间:2023-09-26

我想使用Html。BeginForm条件。代码中有2个字段,如下

@using (Html.BeginForm("Form1Action", "Form1", FormMethod.Post, new { id = "form1" }))
{
   <label for="firstlabel">Label 1</label>
      <input type="text" id="firstlabel" name="firstlabel"/>
   <label for="secondlabel">Label 2</label>
      <input type="text" id="secondlabel" name="secondlabel"/>
   <a href="#" class="button" onclick="SubmitForm();">Submit</a>
}

, JS

<script type="text/javascript">
  function SubmitForm() {
    $('#form1').submit();
  }
</script>

如果secondlabel为空白或为空,那么我想通过另一个操作提交表单,那么我该如何做到这一点呢?

我指的是另一个动作:

 @using (Html.BeginForm("Form2Action", "Form2", FormMethod.Post, new { id = "form2" }))

试试这个…

function SubmitForm() {
if($('#secondlabel').val().length)
    $('#form1').submit();
else
 $('#form2').submit();
  }

或通过改变动作

    function SubmitForm() {
    if($('#secondlabel').val().length == 0)
        $('#form1').attr('action', $('#form2').attr('action'));

  $('#form1').submit();
      }