简体   繁体   English

这是ASP.NET模型绑定中的错误吗?

[英]Is this a bug in ASP.NET Model binding?

I have a simple form in ASP.NET MVC. 我在ASP.NET MVC中有一个简单的表单。 I am trying to post these results to a controller action and I am getting strange behavior. 我试图将这些结果发布到控制器动作,我的行为很奇怪。

The view is a simple HTML table: view是一个简单的HTML表:

视图的图片

Here is part of the HTML form View: 以下是HTML表单视图的一部分:

 <form action="/Applications/UpdateSurvey" method="post"><table id=questionsTable class=questionsTable border=1>
<thead><tr><td>Name</td><td>Answer</td><td>Name Attribute(for debugging)</td></tr>         </thead><tbody>
 <tr>
 <td>Question 0:</td>

 <td><input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=1 >1&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=2 >2&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=3 >3&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=4 >4&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[0].responseIds' value=5 >5&nbsp;&nbsp;</td>
 <td>updater.questions[0].responseIds</td>
 </tr>
 <tr>
 <td>Question 1:</td>
 <td><input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=1 >1&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=2 >2&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=3 >3&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=4 >4&nbsp;&nbsp;<input type='checkbox' class='checkboxes' name='updater.questions[1].responseIds' value=5 >5&nbsp;&nbsp;</td>

 <td>updater.questions[1].responseIds</td>
 </tr>
 </tbody></table>

  <input type="submit" value="Save" />

 </form>

Binding Object: 绑定对象:

public class SurveyUpdater
{
    public Question[] questions { get; set; }
}

public class Question
{
    public int[] responseIds { get; set; }
}

Controller Action code: 控制器动作代码:

    public ActionResult UpdateSurvey(SurveyUpdater updater)
    {
        if (updater.questions == null)
        {
            //I dont understand why this is getting hit
        }
        if (updater.questions.Length != 5)
        {
            //I dont understand why this is getting hit
        }

        return View("TestSurvey");
    }

After testing, here are my observations: 经过测试,以下是我的观察:

  1. If I have at least one CheckBox selected on each of the questions, this works fine and in my controller updater.questions.Length == 5 and the data binds perfectly. 如果我在每个问题上选择了至少一个CheckBox ,这可以正常工作,并且在我的控制器updater.questions.Length == 5 ,数据绑定完美。

  2. If I don't answer one of the questions at all, I only get an array as big as the number I skipped: -1 . 如果我根本不回答其中一个问题,我只得到一个与我跳过的数字一样大的数组: -1 So if I didn't answer Question #3, I get an array in my controller action of 2. 所以,如果我没有回答问题#3,我的控制器动作为2得到一个数组。

  3. By using the logic of #2, if I don't answer the first question, I simply get null for updater.questions 通过使用#2的逻辑,如果我不回答第一个问题,我只是为updater.questions得到null

What I want to get (and what I expected) is that: 我想得到的(以及我所期望的)是:

I would always get questions with a length of 5 and in the cases where I didn't answer one of the questions, I would simply get a 0 sized array for that index responseIds . 我总是会得到长度为5 questions ,而在我没有回答其中一个问题的情况下,我只会为索引responseIds得到一个0大小的数组。

Is this a bug in ASP.NET MVC model binding? 这是ASP.NET MVC模型绑定中的错误吗? If not, is there anything I am missing or any way to get the desired behavior that I am looking for? 如果没有,我有什么遗漏或任何方式来获得我想要的行为吗?

The problem, I think, is because when no selections are chosen, the inputs are not even passed back in the request parameters. 我认为这个问题是因为当没有选择选择时,输入甚至不会在请求参数中传回。 One way around this would be to have a default, hidden checkbox containing a known value that you could filter out selected initially for each question (a "not answered" checkbox, if you will). 解决这个问题的一种方法是使用一个默认的隐藏复选框,其中包含一个已知值,您可以根据每个问题初步选择一个已知值(如果您愿意,则选择“未应答”复选框)。 That would guarantee that you get a selection for each question and that a request parameter exists for each element in the array. 这将保证您获得每个问题的选择,并且数组中的每个元素都存在请求参数。

Think about it from the perspective of what gets posted back. 从发布回来的角度考虑一下。 Only those elements that have values, have names, and aren't disabled get posted. 仅发布具有值,具有名称且未禁用的元素。 If not all the questions have values, then how many array items should it create? 如果不是所有问题都有值,那么它应该创建多少个数组项? At best it can guess that the last item selected should be the size of the array -- but what values should it use for any items in between? 最多可以猜测所选的最后一项应该是数组的大小 - 但是它应该用于中间任何项目的值是多少? The framework can't read your mind and, arguably, shouldn't though providing the default value for the type might be reasonable. 框架无法读懂您的想法,并且可以说,不应该提供该类型的默认值可能是合理的。 IMO, it would be better to just omit the value and, thus, force the developer to provide the default if desired. IMO,最好省略该值,因此,如果需要,强制开发人员提供默认值。 That seems to be what is happening. 这似乎正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM