3

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

控制器(应该得到 post 操作):

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

3 回答 3

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"/>
}
于 2013-10-04T07:50:00.120 回答
2

The Html.BeginForm helper will create the form tags for you, try it...

View:

@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>
 }
于 2013-10-04T05:11:09.977 回答
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>
}

试一试...

让我知道

于 2014-02-22T13:34:47.663 回答