2013-10-04 46 views
3

试图在MVC4(+ Razor)中实现表单,但提交按钮没有做任何事情。MVC 4表单 - 提交按钮不起任何作用

控制器(应该得到这个职位,动作):

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

[不创建嵌套表格...](HTTP ://stackoverflow.com/questions/379610/can-you-nest-html-forms) –

+0

只是帮助我!谢谢! –

回答

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> 
} 
+0

也不确定我是否遵循 - 我使用Html.BeginForm ... – user1025852

+0

您有两种嵌套形式,所以提交按钮仅适用于内部表单。你永远不会提交外部表单。外部表单是使用Html.BeginForm帮助器创建的,并且始终未被触发。 – Germando

+0

谢谢。 Html.BeginForm应该完全位于

之上?无论如何它没有做到这一点......在这里可能仍然有东西丢失 – user1025852

5

如果你只需要实现搜索你不需要使用视图模型,你可以发送搜索请求的字符串。它不应该是Post方法:

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

    return View(yourmodel); 
} 

在查看

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

谢谢我会尝试。不过,我确实希望自己的代码干净,并且我的搜索有多个输入参数,不想用纯字符串维持搜索。 – user1025852

+0

谢谢,我按照您的建议做了。但它不起作用。只有当我在.cshtml @ {Layout = null}的开头添加时,它才起作用(为了简化事情,我们只需说我有一个没有字段的按钮,并且我想要到达控制器) – user1025852

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> 
} 

给一个尝试...

让我知道