Html.BeginForm不会返回POST actionresult - RAZOR

时间:2015-09-15 15:29:21

标签: html asp.net-mvc asp.net-mvc-4 razor

我已经创建了一个视图来输入结果并反馈到控制器,我可以将它们保存到数据库中,但是当我单击提交时,我将返回给GET actionresult,我尝试将BeginForm放入其中确实有效但它通过将所有输入文本框塞在彼此旁边完全改变了我视图中的显示。如何保留现有布局并提交到POST动作结果?`



@{
    ViewBag.Title = "CreateLecturer";
}

<br /><br /><br /><br />
<form class="form-horizontal">
    @using (Html.BeginForm("CreateLecturer", "Admin", new { }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
  <fieldset>
    <legend>Add Lecturer</legend>
    
    <div class="form-group">
      <label for="addLect" class="col-lg-2 control-label">Lecturer Number</label>
      <div class="col-lg-4">
        <input type="text" class="form-control" id="addLect" name="addLect" placeholder="Lecturer Number">
      </div>
    </div>
       <div class="form-group">
      <label for="addLectFname" class="col-lg-2 control-label">Firstname</label>
      <div class="col-lg-4">
        <input type="text" class="form-control" id="addLectFname" name="addLectFname" placeholder="Firstname">
      </div>
    </div>
       <div class="form-group">
      <label for="addLectSname" class="col-lg-2 control-label">Surname</label>
      <div class="col-lg-4">
        <input type="text" class="form-control" id="addLectSname" name="addLectSname" placeholder="Surname">
      </div>
    </div>
       <div class="form-group">
      <label for="addLectPword" class="col-lg-2 control-label">Password</label>
      <div class="col-lg-4">
        <input type="password" class="form-control" id="addLectPword" name="addLectPword" placeholder="Password">
      </div>
    </div>
   
    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
         @Html.ActionLink("Cancel", "ViewLecturer", "Admin", null, new { @class = "btn btn-warning" })
        <button type="reset" class="btn btn-default">Clear</button>
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </div>
      
  </fieldset>
   }
 </form>      
&#13;
&#13;
&#13;

`

1 个答案:

答案 0 :(得分:2)

您现在有两种表格..您不能同时使用<form>Html.BeginForm。你需要使用其中一个。

尝试

@using (Html.BeginForm("CreateLecturer", "Admin", new { }, FormMethod.Post, new { @class="form-horizontal", enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <fieldset>
        <legend>Add Lecturer</legend>

        <div class="form-group">
            <label for="addLect" class="col-lg-2 control-label">Lecturer Number</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" id="addLect" name="addLect" placeholder="Lecturer Number">
            </div>
        </div>
        <div class="form-group">
            <label for="addLectFname" class="col-lg-2 control-label">Firstname</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" id="addLectFname" name="addLectFname" placeholder="Firstname">
            </div>
        </div>
        <div class="form-group">
            <label for="addLectSname" class="col-lg-2 control-label">Surname</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" id="addLectSname" name="addLectSname" placeholder="Surname">
            </div>
        </div>
        <div class="form-group">
            <label for="addLectPword" class="col-lg-2 control-label">Password</label>
            <div class="col-lg-4">
                <input type="password" class="form-control" id="addLectPword" name="addLectPword" placeholder="Password">
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-10 col-lg-offset-2">
                @Html.ActionLink("Cancel", "ViewLecturer", "Admin", null, new { @class = "btn btn-warning" })
                <button type="reset" class="btn btn-default">Clear</button>
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>

    </fieldset>
}

您的另一个选择是删除@using BeginForm,然后使用

<form class="form-horizontal" action="@Url.Action("CreateLecturer","Admin")" enctype="multipart/form-data" method="post">

</form>