MVC 4表格 - 提交按钮不会做任何事情 [英] MVC 4 Form - submit button doesn't do anything

查看:142
本文介绍了MVC 4表格 - 提交按钮不会做任何事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图实现MVC4(+剃刀)形式,但提交按钮没有做任何事情。

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

 公共类GeneralController
{
    [HttpPost]
    公众的ActionResult SearchResult所(SearchParamsModel searchParams)
    {
        //干什么一些东西在这里
        返回视图(SearchResultsView);
    }
}

视图(.cshtml)

  @model Models.SearchParamsModel
@using(Html.BeginForm(SearchResult所,一般,FormMethod.Post))
{
    <节类=表单域>
        <输入类型=文本名称=Property1ID =Property1级=字段field139自动完成-的init-NO-IMG/>
        <标签=Property1>&VALUE1 LT; /标签>        <形式的行动=方法=后级=clearfix>
            <输入类型=提交值=一些价值级=提交BTN蓝BTN特殊提交/>
        < /表及GT;
    < /节>
}

型号

 公共类SearchParamsModel
{
    公共字符串Property1 {搞定;组; }
}


解决方案

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

 公众的ActionResult SearchResult所(字符串搜索字符串)
{
    // code搜索    返回查看(yourmodel);
}

在View

  @using(Html.BeginForm())
{
    搜索:@ Html.TextBox(SearchString在)
    <输入类型=提交值=搜索/>
}

Trying to implement a form in MVC4 (+Razor), but submit button is not doing anything.

Controller (that should get the post action):

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

View (.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>
}

Model

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

解决方案

If you just need to implement searching you don't need to use ViewModel, you may send string with search request. And it shouldn't be Post method:

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

    return View(yourmodel);
}

In View

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

这篇关于MVC 4表格 - 提交按钮不会做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆