MVC 4表单 - 提交按钮不执行任何操作

时间:2013-10-04 05:02:02

标签: asp.net-mvc-4 post submit

尝试在MVC4(+ Razor)中实现表单,但是提交按钮没有做任何事情。

控制器(应该接受后期操作):

public class GeneralController
{
    [HttpPost]
    public ActionResult SearchResults(SearchParamsModel searchParams)
    {
        // doin some stuff here
        return View("SearchResultsView");
    }
}

查看(.cshtml)

@model Models.SearchParamsModel 
@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
{
    <section class="form-field">
        <input type="text" name="Property1" id="Property1" class="field field139 autocomplete-init-no-img" />
        <label for="Property1">value1</label>

        <form action="" method="post" class="clearfix">           
            <input type="submit" value="some value" class="submit btn blue-btn special-submit" />
        </form>
    </section>
}

模型

public class SearchParamsModel 
{
    public string Property1{ get; set; }
}

3 个答案:

答案 0 :(得分:5)

如果您只需要实现搜索,则不需要使用ViewModel,您可以发送带搜索请求的字符串。它不应该是Post方法:

public ActionResult SearchResults(string searchString)
{
    //code for searching

    return View(yourmodel);
}

在视图中

@using (Html.BeginForm())
{     
    Searching: @Html.TextBox("SearchString")
    <input type="submit" value="Search"/>
}

答案 1 :(得分:2)

Html.BeginForm助手会为你创建表单标签,试试吧......

查看:

@model Models.SearchParamsModel 

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
 {
  <section class="form-field">
    <input type="text" name="Property1" id="Property1" class="field
                  field139 autocomplete-init-no-img" />
    <label for="Property1">value1</label>
    <input type="submit" value="some value" 
                    class="submit btn blue-btn special-submit" />   
  </section>
 }

答案 2 :(得分:1)

如果我在MVC 4或5中做同样的事情,我会得到相同的结果。

查看在所有控件周围添加<fieldset>标记:

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
{
  <fieldset>
  // your controls...
  <input type="text" name="Property1" id="Property1" class="field field139 autocomplete-init-no-img" />
  <label for="Property1">value1</label>
  <input type="submit" value="some value" class="submit btn blue-btn special-submit" />      
  // if you need a partial form included:
  @{Html.RenderPartial("_SomeOtherPartial", @Model);}
  // etc..
  </fieldset>
}

尝试一下......

让我知道