简体   繁体   English

在ASP.NET MVC3视图的for循环中生成具有相同名称的RadioButtons列表

[英]Generating a list of RadioButtons with the same name in a for-loop in an ASP.NET MVC3 view

I'm working on an ASP.NET MVC3 project, and I have a problem with RadioButtons name generated by a for loop in a PartialView. 我正在开发一个ASP.NET MVC3项目,并且PartialView中的for循环生成的RadioButtons名称存在问题。
First, here is my code, I'll explain the exact problem just after : 首先,这是我的代码,下面将解释确切的问题:

Model FileUploadModel 模型FileUploadModel

public class FileUploadModel
{
    public HttpPostedFileBase File { get; set; }

    public bool IsMainFile { get; set; }
}


Model MyModel 模特MyModel

public class MyModel
{
    // Some properties

    public List<FileUploadModel> Files { get; set; }
}


How the PartialView _UploadFiles is called 如何调用PartialView _UploadFiles

@Html.Partial("_UploadFiles", Model) // Model is a MyModel model


PartialView _UploadFiles - HERE IS THE PROBLEM PartialView _UploadFiles-这里是问题

@model MyModel

@{
    var nbFiles = (Model.Files != null ? Model.Files.Count : 0);
    const int NB_DEFAULT_UPLOAD_FIELDS = 3;
}

@for (int i = 0; i < NB_DEFAULT_UPLOAD_FIELDS; i++)
{
    @Html.TextBoxFor(m => m.Files[nbFiles].File, new { type = "file", name = "Files", id = "Files" })
    @Html.RadioButtonFor(m => m.Files[nbFiles].IsMainFile, "Main ?", new { id = "rb" + i + "_File_IsMain" })
}

So this is the last snippet the problem. 因此,这是问题的最后片段。 I'd like to create 3 upload fields with an associated RadioButton indicating if the file is the "main file". 我想用关联的RadioButton创建3个上传字段,以指示文件是否为“主文件”。 However, with the above snippet, the displayed view is OK, but when I validate my form, my posted model only have the first picture uploaded (and the corresponding boolean IsMainFile , the others are just ignored). 但是,使用上面的代码片段,显示的视图就可以了,但是当我验证表单时,我发布的模型仅上传了第一张图片(以及相应的布尔值IsMainFile ,其余的则被忽略了)。 That means the List Files contains only the first TextBox and first RadioButton data 这意味着列表Files仅包含第一个TextBox和第一个RadioButton数据

So I tried with @Html.RadioButtonFor(m => m.Files[i].IsMainFile , but the RadioButtons have a different name so user can check all RadioButtons, which is NOT the desired behavior. MVC doesn't authorize me to override the name of the RadioButtons, so I can't give them my own name. 因此,我尝试使用@Html.RadioButtonFor(m => m.Files[i].IsMainFile ,但是RadioButton的名称不同,因此用户可以检查所有RadioButton,这不是理想的行为@Html.RadioButtonFor(m => m.Files[i].IsMainFile没有授权我覆盖RadioButtons的name ,所以我不能给他们自己的名字。

How can I generate these fields is order to only ONE Radiobutton can be checked AND my MyModel.Files property contains all chosen files ? 我如何生成这些字段,以便仅可以检查一个MyModel.Files属性包含所有选择的文件?

Thank you 谢谢

I think your best bet here is to use editor templates . 我认为您最好的选择是使用编辑器模板

First, you create a view named FileUploadModel , and place it a folder named EditorTemplates which should exist under your controller's Views folder. 首先,创建一个名为FileUploadModel的视图,并将其放置在一个名为EditorTemplates的文件夹中,该文件夹应位于控制器的Views文件夹下。 Note that the name of the view must match the name of your view model's class. 请注意,视图的名称必须与视图模型的类的名称匹配。

Its contents would look something like: 其内容如下所示:

@model FileUploadModel

@Html.TextBoxFor(m => m.File, new { type = "file" })
@Html.RadioButtonFor(m => m.IsMainFile, true)

Then, back in your "_UploadFiles" view, your markup for the input field and radio button would change to: 然后,回到"_UploadFiles"视图,输入字段和单选按钮的标记将更改为:

@model MyModel

using (Html.BeginForm(...

    @Html.EditorFor(m => m.Files)

...

Note that this will automatically iterate through and apply that editor template for each FileUploadModel object, and automatically name them as required. 请注意,这将自动迭代并为每个FileUploadModel对象应用该编辑器模板,并根据需要自动为其命名。 This of course assumes that the Files property in your model is populated. 当然,这假定已填充模型中的Files属性。

Your action that accepts the post should accept type MyModel as its sole parameter, and everything should bind up automatically at post. 您接受帖子的操作应接受MyModel类型作为其唯一参数,并且所有内容都应在发布时自动绑定。

There are ways to avoid editor templates and build and name those fields programmatically , but this is really the preferred way, and much cleaner. 有一些方法可以避免使用编辑器模板, 并以编程方式构建和命名这些字段 ,但这确实是首选方法,而且更加简洁。

More specific to your issue... (In other words, I forgot that the code above won't group radio buttons.) 更具体地针对您的问题... (换句话说,我忘记了上面的代码不会对单选按钮进行分组。)

For the grouping of radio buttons to work, you should set the attribute name as follows: 为了使单选按钮分组有效,应按如下所示设置属性名称:

@Html.RadioButtonFor(m => m.IsMainFile, true, new { Name = "IsMainFile" })

Case matters here. 案例很重要。 Note that Name must be capitalized for this to work. 请注意, Name必须大写才能生效。 I'm not sure why this is the case. 我不确定为什么会这样。

The problem is that once you change that attribute, this field will no longer automatically bind in the post. 问题在于,一旦更改了该属性,该字段将不再自动绑定到帖子中。 This is unfortunate but understandably is as designed since, if you look at the request, I believe you will see that only the selected radio button value has been posted. 不幸的是,但这是设计好的,因为如果您查看请求,我相信您会看到仅发布了选定的单选按钮值。

So, you could change the value of the radio button to something recognizable since just that one instance of IsMainFile will show up in the request, if I'm correct. 因此,您可以将单选按钮的值更改为可识别的值,因为如果我是对的,则只是IsMainFile一个实例将显示在请求中。

Perhaps you can use this specific knowledge to tweak your existing code. 也许您可以使用此特定知识来调整现有代码。

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

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