单选按钮,复选框,并选择元素没有得到相应的填充 [英] Radiobutton, checkbox and select elements don't get populated accordingly

查看:160
本文介绍了单选按钮,复选框,并选择元素没有得到相应的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种不同的元素 - 文本框,选择,复选框和单选按钮上我的看法。当我present与模型这个强类型的观点有一些得到填充相应的模型值但有一定种类和那些不某些元素。单选按钮,复选框是空白。当谈到选择元素,如果它们与 @ Html.DropDownListFor helper方法,创建然后他们得到正确的价值观,但如果他们只是普通的HTML对应于他们的名字选择模特属性则默认 - 第一个项目被选中。我创建了两个复选框和单选按钮与@Html辅助方法:

  @ Html.RadioButtonFor(X =>,X preferredReplyMethod,preferredReplyMethod.Written.GetHash code(),新的{ID =rbReplyWritten })
            <标签=rbReplyWritten>书写和LT; /标签>
            @ Html.RadioButtonFor(X =>,X preferredReplyMethod,preferredReplyMethod.Verbal.GetHash code(),新的{ID =rbReplyVerbal})
            <标签=rbReplyVerbal>口头和LT; /标签>         @ Html.CheckBoxFor(X => x.IsUnderControl,新{ID =chkUnderControl})
         @ Html.LabelFor(X => x.IsUnderControl,新{@for =chkUnderControl})

问题是,如果我发布这些值回服务器,所有保存的值是正确的。我应该怎么办?


解决方案

我终于找到了问题的原因。因此,抓住你的咖啡(当然,那些谁是新的ASP.NET MVC模式,像我这样的结合),以SIP作为你读。正如我所说的,我与复选框,单选按钮,选择元件(未用DropDownListFor&LT的帮助下产生的;> helper方法)的问题没有正确的价值观。当回发的表格我能保存所有数据(除复选框)我会正确地输入到数据库中。但看完回来的时候,形式没有被充满了正确的价值观 - 选择有选择他们的第一选择,单选按钮和复选框的地方清晰。问题的常见原因是属性的值没有正确的值。所以,我会通过一个触摸他们每个人之一。


  1. 在一些选择元素均与DropDownListFor&LT创建的视图;> helper方法,而有些人只是标记为普通的HTML他们的名字匹配到相应的模型属性的,就像这样:


 <选择类=选择正常NAME =ApplicationType>
   <期权价值=0> - 选择 - < /选项>
   <期权价值=1>投诉与LT; /选项>
   <期权价值=2>投标< /选项>
   <期权价值=3>查询< /选项>
< /选择>


ApplicationType是自定义的枚举类型的。

 公共枚举ApplicationType {
     投诉= 1
     提案= 2,
     查询= 3
   }

当在编辑/插入模式中选择的选项很容易绑定到相应的属性,因为有一个从数值到枚举隐式转换。但阅读时,模型属性值是枚举成员,而不是它的潜在价值的名称。所以,即,如果ApplicationType是ApplicationType.Proposal MVC引擎将查找具有相同的名称和会发现我们选择的元素。然后,它会试图找到一个具有建议,而不是2的值的选项,它不会狼狈不堪。因此,我们必须将值设置为枚举选项的名称,如:


 <选择类=选择正常NAME =ApplicationType>
   <期权价值=0> - 选择 - < /选项>
   <期权价值=投诉​​>投诉与LT; /选项>
   <期权价值=建议>投标< /选项>
   <期权价值=查询>查询< /选项>
< /选择>


但即使仅此还不足以让选择正确的选项。问题是,对于要选择一个选项,它需要属性进行设置,这样选择=选择。不幸的是我无法弄清楚如何做到这一点,所以我在网上搜索,发现了有一个在MVC 5.1新的HTML扩展方法caled EnumDropDownListFor<>,你可以从的此处。这种方法生成由枚举一个HTML选择元素。你要记住两件事在使用它之前:

a)你需要Disaplay(NAME =Enum1显示文本),如果你想装饰你的枚举选项的选择显示用户友好的名称
 二)在默认情况下该方法将设置选项元素的值是枚举的潜在价值。只要改变这个

 字符串值= Convert.ChangeType(field.GetValue(空),baseEnumType)的ToString();

这个

 字符串值= field.Name;

添加此code到项目中,哟必须要做的仅仅是添加code的这一行的唯一的事情后,

  @ Html.EnumDropDownListFor(X => x.ApplicationType,新{@class =选择正常})

不过,当然,如果你设法以某种方式控制选项selectedness,你仍然可以去老样子。这就是全部,你是好去。


  1. 有关单选按钮我的错误是将值设置为Enum.GetHash code()。因此,改变这种

    @ Html.RadioButtonFor(X =>的X. preferredReplyMethod,preferredReplyMethod.Verbal.GetHash code())


这将解决这个问题。


  

@ Html.RadioButtonFor(X =>的X. preferredReplyMethod,
  preferredReplyMethod.Verbal .GetHash code()



  1. 通过复选框不过,情况有点不同。如果我通过模型到Html.BeginForm方法明确地这样

    @using(Html.Beginform(SaveDocument,DocumentController,型号))


和调试操作方法我可以看到对应的复选框(他们所有类型的 INT )的模型属性都为0值。所以,当他们读回这将导致复选框取消选中。我一直无法找出原因,因为如果我让MVC引擎通过模型含蓄,这个问题蒸发。

  @using(Html.Beginform(SaveDocument,DocumentController))

I have different kinds of elements - textboxes, selects, checkboxes and radio buttons on my view. When I present this strongly typed view with the model there are some elements that get populated with corresponding model values but there are certain kinds and ones that don't. Radio buttons, checkboxes are blank. When it comes to select elements, if they are created with @Html.DropDownListFor helper method then they get correct values, but if they are just plain Html selects with their names corresponding to model properties then the default- first item is selected. I've created both checkboxes and radiobuttons with @Html helper methods:

              @Html.RadioButtonFor(x => x.PreferredReplyMethod, PreferredReplyMethod.Written.GetHashCode(), new { id = "rbReplyWritten" })
            <label for="rbReplyWritten">Written</label>
            @Html.RadioButtonFor(x => x.PreferredReplyMethod, PreferredReplyMethod.Verbal.GetHashCode(), new { id = "rbReplyVerbal" })
            <label for="rbReplyVerbal">Verbal</label>

         @Html.CheckBoxFor(x => x.IsUnderControl, new { id = "chkUnderControl" })
         @Html.LabelFor(x => x.IsUnderControl, new { @for = "chkUnderControl" })

The thing is, if I post these values to back to the server, all the saved values are correct. What should I do?

解决方案

I finally found the cause of the problem. So grab your coffee (of course those who are new to ASP.NET MVC model binding like me) to sip as you read. As I said I had a problem with the checkboxes, radiobuttons and select elements (that were not generated with the help of DropDownListFor<> helper method) not having correct values. When posting back the form I was able to save all the data (except for checkboxes) I'd entered to the database correctly. But when reading back, the form was not being filled with correct values - selects had their first options selected, radio buttons and checkboxes where clear. The common cause of the problem was the value attribute not having correct value. So I'll touch each of them one by one.

  1. On the view some of the select elements were created with DropDownListFor<> helper method, whereas some were just marked up as plain html matching their name to that of the corresponding model property, just like this:

<select class="select-normal" name="ApplicationType">
   <option value="0">--Choose--</option>
   <option value="1">Complaint</option>
   <option value="2">Proposal</option>
   <option value="3">Query</option>
</select>

ApplicationType is of custom Enum type.

   public Enum ApplicationType{
     Complaint=1
     Proposal=2,
     Query=3
   }

When in edit/insert mode the selected option is easily bound to the appropriate property because there's an implicit cast from numeric value to enum. But when reading, the model property value is the name of the enum member, not its underlying value. So i.e., if the ApplicationType is ApplicationType.Proposal MVC engine will look for the element that has the same name and will find our select. Then it will try to find the option that has the value of "Proposal",not 2, and it won't find any. So we have to set the values to the names of the enum options, like this:

<select class="select-normal" name="ApplicationType">
   <option value="0">--Choose--</option>
   <option value="Complaint">Complaint</option>
   <option value="Proposal">Proposal</option>
   <option value="Query">Query</option>
</select>

But even this alone is not enough to get the correct option selected. The problem is, for an option to be selected it needs selected attribute to be set, as such selected="selected". Unfortunately I couldn't figure out how to accomplish this, so I searched the web and found out that there's a new Html extension method in MVC 5.1 caled EnumDropDownListFor<>, which you can add to your project from here. This method generates an html select element from an enum. You have to keep two things in mind before using it:

a) You need to decorate your enum options with Disaplay(Name="Enum1 display text") if you want your select to display user friendly names b) By default the method will set the value of the option element to be the underlying value of the enum. Just change this

string value = Convert.ChangeType(field.GetValue(null), baseEnumType).ToString();

to this

string value=field.Name;

After adding this code to your project, the only thing yo have to do is just add this one line of code

@Html.EnumDropDownListFor(x => x.ApplicationType, new { @class = "select-normal" })

But of course, if you manage to control the "selectedness" of options somehow, you can still go the old way .That's all, you're good to go.

  1. For radiobuttons my mistake was to set the value to Enum.GetHashCode(). So changing this

    @Html.RadioButtonFor(x => x.PreferredReplyMethod, PreferredReplyMethod.Verbal.GetHashCode())

to this will solve the problem

@Html.RadioButtonFor(x => x.PreferredReplyMethod, PreferredReplyMethod.Verbal.GetHashCode())

  1. With checkboxes though, the situation is a bit different. If I pass the Model to the Html.BeginForm method explicitly like this

    @using(Html.Beginform("SaveDocument","DocumentController",Model))

and debug the action method I can see that the model properties corresponding to checkboxes (they're all of type int) have value of "0". So when they're read back this causes the checkboxes to be unchecked. I haven't been able to figure out why, because if I let the MVC engine pass the model implicitly, this problem evaporates.

@using(Html.Beginform("SaveDocument","DocumentController"))

这篇关于单选按钮,复选框,并选择元素没有得到相应的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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