【问题标题】:ASP.NET MVC Submit form data into databaseASP.NET MVC 提交表单数据到数据库
【发布时间】:2017-11-02 07:14:12
【问题描述】:

我有一个 ASP.NET mvc 应用程序,其视图如下:

@using (Html.BeginForm("FormSubmit", "HomeController", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal" }))
    {    
            <fieldset>

                <!-- Form Name -->
                <legend>Survey Form</legend>

                <br>
                <br>

                 <centre> <p> This is a survey for User X. <p>
                 </centre>

                    <!-- Multiple Radios -->
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="q1">Any Plans to attend college?</label>
                        <div class="col-md-4">
                            <div class="radio">
                                <label for="q1-0">
                                    <input type="radio" name="Q1" id="q1-0" value="1">
                                    Yes
                                </label>
                            </div>
                            <div class="radio">
                                <label for="q1-1">
                                    <input type="radio" name="Q1" id="q1-1" value="2">
                                    No
                                </label>
                            </div>
                        </div>
                    </div>

      <input type="submit" name="SaveButton" value="Save" align="center">  

       </fieldset>

 }

我在 HomeController 中创建了控制器:

   [HttpPost]
   public ActionResult FormSubmit(int Q1)
    {
        return Content(Request.Form["SaveButton"]);
       //  return RedirectToAction("Index");
    }

现在当我尝试这个按钮时,它给了我一个:

  Server Error in '/surveyapps' Application.
  The resource cannot be found.

我完全按照 stack-overflow 上的帖子中所说的那样做:asp.net mvc form not posting parameter values

有什么办法可以解决这个错误?我确定如果我得到表单值,那么我可以将它们插入数据库。

【问题讨论】:

  • 你为什么使用“SaveButton”? (在 Content(Request.Form["SaveButton"]);) - 您试图从提交 btn 中读取值 - 没有意义。
  • 不,它只是为了看到我从表单发布操作中获得了一些价值......否则没有任何意义。
  • 您可以尝试删除此行:new { ReturnUrl = ViewBag.ReturnUrl }
  • 只需在 beginform 中将“HomeController”更改为“Home”即可...
  • 哦,没看过

标签: c# asp.net asp.net-mvc forms asp.net-mvc-4


【解决方案1】:

在您看来,将 HomeController 更改为 Home

像这样:

@using (Html.BeginForm("FormSubmit", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal" }))

框架会自动使用“Controller”后缀来处理这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2021-12-14
    • 2019-01-19
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多