Html.BeginForm 不会返回 POST 操作结果 - RAZOR

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

我已经创建了一个 View 来输入结果并反馈给 Controller ,我可以将它们保存到数据库中,但是当我单击提交时,我将返回到 GET actionresult,我尝试将 BeginForm 放入其中并且它确实有效,但是它通过将所有输入文本框塞在一起而完全改变了我 View 中的显示。如何保留现有布局并提交到 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>      

`

最佳答案

您现在有两个表单..您不能使用 <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>

关于Html.BeginForm 不会返回 POST 操作结果 - RAZOR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589987/

相关文章:

html - 如何 flex 基础 : 0% and flex-grow factor calculates the computed width?

html - Bootstrap 4 Navbar-品牌垂直对齐

javascript - TrimStart()在文本框中动态空格,ASP.NET MVC

c# - 根据 Razor MVC 4 中的条件从 Controller 控制局部 View

javascript - 当 jqgrid 编辑使用 web api 获取数据时,无法将数据绑定(bind)到 jqgrid 中的下拉列表

asp.net-mvc-4 - MVC4 中何时设置 User.Identity.Name

javascript - 使用JS动态添加下拉列表表

javascript - 将脚本放在头部和 body 中有什么区别?

c# - MVCMailer SendAsync() 因 Amazon SES 而失败

c# - 使用 Ninject 时如何模拟通用 Get 方法并使用 SetUp 方法填充模拟数据库?