当视图使用主布局时,MVC 4 表单提交按钮不起作用 [英] MVC 4 form submit button not working when view is using master layout

查看:10
本文介绍了当视图使用主布局时,MVC 4 表单提交按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,经过长时间的调查,似乎当我创建了一个与 _layout.cshtml 一起使用的视图时 - 表单中的提交按钮不起作用(没有操作返回给控制器)​​.

ok, after long investigation , it seems like when I have a view that was created to work with the _layout.cshtml - the submit button in the form I have doesn't work (no action is returned to controller).

仅当我创建一个视图并取消选中使用布局或母版页"时 - 按钮才起作用!

Only when I created a view and unchecked "Use a layout or master page" - the button has worked!

这似乎非常不清楚,所以 - 我怎么能同时看到一般 _layout.cshtml 和 工作表单按钮?

This seems extremely unclear, so - how can I have both view with the general _layout.cshtml alongside with a working form button?

如下:尝试在MVC4(+Razor)中实现一个表单

below: Try to implement a form in 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; }
    }

推荐答案

你应该删除你的内部表单标签,

You should remove your inner form tag,

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))

将为您生成一个表单标签.

will generate a form tag for you.

此外,您应该使用 html helper 来生成表单元素:

Also, you should use the html helpers to generate your form elements:

@Html.LabelFor(model => model.Property1)
@Html.TextBoxFor(model => model.Property1)

因此可能是模型绑定问题.提交按钮属于您嵌套的内部表单,这里没有正在提交的模型.

Could be a possible model binding issue due to this. The submit button belongs to your nested inner form, there is no model that is being submitted here.

@model MvcApplication2.Models.SearchParamsModel

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
   <section class="form-field">
      @Html.LabelFor(model => model.Property1)
      @Html.TextBoxFor(model => model.Property1)
      <input type="submit" value="some value" class="submit btn blue-btn special-submit" />
   </section>
}

这篇关于当视图使用主布局时,MVC 4 表单提交按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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