我怎样才能得到复选框保持ASP.NET MVC跨回传选中状态? [英] How can I get CheckBox to maintain checked state across postback in ASP.NET MVC?

查看:94
本文介绍了我怎样才能得到复选框保持ASP.NET MVC跨回传选中状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式的ASP.NET MVC项目。在处理POST谓词操作方法我有一个自定义的 IModelBinder 的实施是表单数据绑定到我的模型实例。如果有我使用错误 bindingContext.ModelState.SetAttemptedValue() bindingContext.ModelState.AddModelError()坚持提交的值和错误信息到的ModelState

这个伟大的工程,我可以看到预期的行为上呈现与 Html.TextBox我输入控件发生()(它要求通过对 Html.InputHelper())。当我使用 Html.CheckBox()(也称通过对 Html.InputHelper())的状态我复选框不会输出到个<输入/方式> 标签

这就像 Html.InputHelper()方法不使用从ModelState中的AttemptedValue类型CheckBox的输入栏,似乎给我。

下面是从<一的code href=\"http://www.$c$cplex.com/aspnet/SourceControl/FileView.aspx?itemId=288010&changeSetId=17272\"相对=nofollow> ASP.NET MVC Html.InputHelper()方法。

为什么该复选框attemptedValue不是输出到输入标签。是否有我丢失的东西在这里还是需要通过检查的ModelState和设置标签手动处理这种情况下属性自己?

更新11/09

下面是我使用的输出复选框 HtmlHelpers 电话:

 &LT;%= Html.CheckBox(IsDerived)%&GT;

这是我使用的注册尝试值调用:

 字符串isDerivedRequestValue =!string.IsNullOrEmpty(bindingContext.HttpContext.Request [IsDerived])? bindingContext.HttpContext.Request.Form.GetValues​​(IsDerived)[0]:空;
bindingContext.ModelState.SetAttemptedValue(IsDerived,isDerivedRequestValue);


解决方案

我不知道这是否是解决问题或不是最好的方式,但由于 Html.InputHelper()方法不检查CheckBox控件的AttemptedValue添加以下到我的控制器中嵌入从正确的价值的ModelState ViewData的,似乎做的伎俩相当不错。

 计算机[IsDerived] = ViewData.ModelState.ContainsKey(IsDerived)
                           ? bool.Parse(ViewData.ModelState [IsDerived。AttemptedValue)
                           :假的;

请确保你没有明确设置器isChecked 参数值,当你调用 Html.CheckBox()作为将覆盖存储在的ViewData 任意值。

I have an ASP.NET MVC project with a form. In the Action method that handles the POST verb I have a custom IModelBinder implementation that is binding the form data to my model instance. If there are errors I am using bindingContext.ModelState.SetAttemptedValue() and bindingContext.ModelState.AddModelError() to persist the submitted value and error message to the ModelState.

This works great and I can see the expected behavior occurring on my input controls that are rendered with Html.TextBox() (which calls through to Html.InputHelper()). When I use Html.CheckBox() (which also calls through to Html.InputHelper()) the state of my CheckBox is NOT output to th <input /> tag.

It seems to me like the Html.InputHelper() method is not using the AttemptedValue from the ModelState for input fields of type CheckBox.

Here is the code from ASP.NET MVC Html.InputHelper() method.

Why is it that the CheckBox attemptedValue is not output to the input tag. Is there something I am missing here or do I need to manually handle this case by checking the ModelState and setting the tag attribute myself?

Update 11/09
Here is the call to HtmlHelpers that I am using to output the CheckBox:

<%= Html.CheckBox("IsDerived") %>

And here is the call that I am using to register the Attempted Value:

string isDerivedRequestValue = !string.IsNullOrEmpty(bindingContext.HttpContext.Request["IsDerived"]) ? bindingContext.HttpContext.Request.Form.GetValues("IsDerived") [0] : null;
bindingContext.ModelState.SetAttemptedValue("IsDerived", isDerivedRequestValue);

解决方案

I'm not sure if this is the best way to solve the problem or not, but since the Html.InputHelper() method is not checking the AttemptedValue for CheckBox controls I added the following to my controller which embeds the proper value from the ModelState into ViewData and seems to do the trick quite well.

ViewData["IsDerived"] = ViewData.ModelState.ContainsKey("IsDerived")
                           ? bool.Parse(ViewData.ModelState["IsDerived"].AttemptedValue)
                           : false;

Make sure you are not explicitly setting the isChecked parameter value when you call Html.CheckBox() as that will override any value stored in ViewData.

这篇关于我怎样才能得到复选框保持ASP.NET MVC跨回传选中状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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