为什么我的复选框映射到一个MVC模型成员? [英] Why doesn't my checkbox map to an MVC model member?
问题描述
我想实现什么这个答案意味着但没有在我的模型code存储的显示名称,所以我相信这是一个单独的问题。
I'm trying to implement what this answer suggests but without storing the display name in my model code, so I believe it's a separate question.
我有一个MVC视图
<%@ Page Language="C#" MasterPageFile="PathToMaster" Inherits="System.Web.Mvc.ViewPage<ModelData>" %>
和模型:
public class ModelData {
public bool Option1 { get; set; }
}
和我有HTML标记的一种形式,它包含有一个复选框的.aspx:
and I have a .aspx with HTML markup for a form that contains a checkbox:
<label for="MyCheckbox">Your choice</label>
<%= Html.CheckBoxFor(Model=>Model.Option1, new { id="Option1", @class="checkbox", onchange="return myValidation();", name="MyCheckbox", value="Option one" } ) %>
它编译收益率时,此HTML:
which when it is compiled yields this HTML:
<label for="MyCheckbox">Your choice</label>
<input class="checkbox" id="Option1" name="Option1" onchange="return myValidation();" type="checkbox" value="Option one" /><input name="Option1" type="hidden" value="false" />
,每当我选中复选框,并提交表单控制器动作
and whenever I check the checkbox and submit the form the controller-action
class MyController : Controller {
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult RequestStuff( ModelData data )
{
}
}
收到数据
,其中选项1
是假
。
我是什么做错了吗?如何让我的复选框,映射到模型成员?
What am I doing wrong? How do I make the checkbox map to the model member?
推荐答案
既然你说改变别名助手EX pression没有削减它,我花了时间prepare的MVC2测试项目,这里是它的外观:
Since you said changing the alias in the helper expression didn't cut it, I took the time to prepare the MVC2 test project, here's how it looks:
MyModel.cs:
MyModel.cs:
namespace TestAppMVC2.Models
{
public class MyModel
{
public bool Option1 { get; set; }
}
}
控制器的操作:
Controller's actions:
public ActionResult MyAction()
{
var viewModel = new MyModel();
return View(viewModel);
}
[HttpPost]
public ActionResult MyAction(MyModel viewModel)
{
bool option = viewModel.Option1;
return View();
}
视图(MyAction.aspx):
The view (MyAction.aspx):
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestAppMVC2.Models.MyModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MyAction
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>MyAction</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Option1) %>
</div>
<div class="editor-field">
<%: Html.CheckBoxFor(model => model.Option1) %>
<%: Html.ValidationMessageFor(model => model.Option1) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
看起来是这样的:
Looks like this:
和检查复选框后的结果:
And the result after checking the checkbox:
顺便说一句,在视图生成全部(当然,几乎完全是,我改变了 TextBoxFor
到 CheckBoxFor
)通过添加视图 - >强类型显示为为MyModel类模板:编辑
By the way, the View was generated entirely (well, almost entirely, I changed TextBoxFor
to CheckBoxFor
) by Add View -> Strongly Typed View for the MyModel class and template: Edit.
因此,它看起来像上面的结合应该没问题。试试这个,那么你就可以与复选框自定义选项摆弄。 :)
So it looks like the binding above should be OK. Try this and then you can fiddle with custom options on checkboxes. :)
就像我写的<一href="http://stackoverflow.com/questions/10649454/how-do-i-map-checkboxes-onto-mvc-model-members/10649724#10649724">in对方的回答,这个想法基本上是一样的,只是在MVC3剃刀语法看起来有点清洁。但仅此而已,当涉及到差异。 :)
Like I wrote in the other answer, the idea is essentially the same, only in MVC3 the Razor syntax looks a bit cleaner. But that's about it when it comes to differences. :)
这篇关于为什么我的复选框映射到一个MVC模型成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!