MVC 4 \\形式提交按钮不是当视图是使用总体布局工作 [英] MVC 4 \ form submit button not working when view is using master layout
问题描述
好了,长时间的调查后 ,它看起来像当我有创建与_layout.cshtml工作的一个观点 - 在我不工作(不采取行动将返回到控制器)
的形式提交按钮只有当我创建一个视图,并选中使用布局或母版页! - 按钮已制定
这似乎非常清楚,所以 - 我怎么能有一个<一旁边的一般_layout.cshtml同时查看href=\"http://stackoverflow.com/questions/19173580/mvc-4-form-submit-button-doesnt-do-anything\">working表单按钮?
如下:
尝试实施MVC4形式(+剃刀)
控制器(应该得到这个职位的动作):
公共类GeneralController {
[HttpPost]
公众的ActionResult SearchResult所(SearchParamsModel searchParams)
{
//干什么一些东西在这里
返回视图(SearchResultsView);
}
}
视图(.cshtml)
@model Models.SearchParamsModel
@using(Html.BeginForm(SearchResult所,一般,FormMethod.Post))
{
&lt;节类=表单域&GT;
&LT;输入类型=文本名称=Property1ID =Property1级=字段field139自动完成-的init-NO-IMG/&GT;
&LT;标签=Property1&GT;&VALUE1 LT; /标签&gt;
&LT;形式的行动=方法=后级=clearfix&GT;
&LT;输入
类型=提交
值=一些价值
类=提交BTN蓝BTN特殊提交/&GT;
&LT; /表及GT;
&LT; /节&gt;
}
型号
公共类SearchParamsModel
{
公共字符串Property1 {搞定;组; }
}
您应该删除你内心的表单标签,
@using(Html.BeginForm(SearchResult所,一般,FormMethod.Post))
将生成一个表单标签适合你。
此外,您应该使用HTML助手来生成表单元素:
@ Html.LabelFor(型号=&GT; model.Property1)
@ Html.TextBoxFor(型号=&GT; model.Property1)
或许是一个可能的模型结合的问题,由于这一点。
提交按钮属于你嵌套内部形式,没有了正在这里提交模式。
@model MvcApplication2.Models.SearchParamsModel@ {
ViewBag.Title =指数;
布局=〜/查看/共享/ _Layout.cshtml
}@using(Html.BeginForm(SearchResult所,一般,FormMethod.Post))
&lt;节类=表单域&GT;
@ Html.LabelFor(型号=&GT; model.Property1)
@ Html.TextBoxFor(型号=&GT; model.Property1)
&LT;输入类型=提交值=一些价值级=提交BTN蓝BTN特殊提交/&GT;
&LT; /节&gt;
}
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!
This seems extremely unclear, so - how can I have both view with the general _layout.cshtml alongside with a working form button?
below: Try to implement a form in MVC4 (+Razor)
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; }
}
You should remove your inner form tag,
@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
will generate a form tag for you.
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屋!