[英]Get CheckBox value corresponding to a FileUpload in ASP.NET MVC
I have 3 file uploads and three radio buttons associated with them. 我有3个文件上传和三个与之关联的单选按钮。
All radio buttons are given same name to make them mutually exclusive. 所有单选按钮均具有相同的名称,以使它们互斥。
View 视图
<div>
Upload Files:<br />
<p>
<input type="file" name="resim1" id="resim1" />
<%= Html.RadioButton("defaultresim", "", true, new { id = "defaultresim1" })%><label for="defaultresim1">Set as default</label>
</p>
<p>
<input type="file" name="resim2" id="resim2" />
<%= Html.RadioButton("defaultresim", "", true, new { id = "defaultresim2" })%><label for="defaultresim2">Set as default</label>
</p>
<p>
<input type="file" name="resim3" id="resim3" />
<%= Html.RadioButton("defaultresim", "", true, new { id = "defaultresim3" })%><label for="defaultresim3">Set as default</label>
</p>
</div>
I have my controller logic like this. 我有这样的控制器逻辑。
List<ProductImage> images = new List<ProductImage>();
if (collection != null)
{
for (int i = 0; i < collection.Count; i++)
{
HttpPostedFileBase resim = collection[i];
if (resim != null)
{
ProductImage image = new ProductImage();
byte[] temp = new byte[resim.ContentLength];
resim.InputStream.Read(temp, 0, resim.ContentLength);
image.ImageData = temp;
image.ImageMimeType = resim.ContentType;
// How to get the image that will be marked as default?
//image.IsImageDefault = ??;
images.Add(image);
}
}
}
Here is how my code snippet look like on browser. 这是我的代码段在浏览器上的样子。
I would like to get the image.IsImageDefault
value ideally inside the loop. 我想在循环内理想地获取
image.IsImageDefault
值。
I am fairly new to MVC and still hung with WebForms and I cannot find a way out. 我对MVC相当陌生,但仍然对WebForms感兴趣,因此找不到出路。
What should be done to solve this situation? 解决该情况应采取什么措施?
You could set the value of each radio button to correspond to the index of the file in the collection (eg, 0, 1, 2). 您可以将每个单选按钮的值设置为对应于集合中文件的索引(例如0、1、2)。 Then, when you loop through the files, check whether your variable
i
is the same as the value of the defaultresim
form field. 然后,当您遍历文件时,请检查变量
i
是否与defaultresim
表单字段的值相同。
Without seeing your method signature, one way of doing this is just to retrieve the defaultresim
value from the form collection: 没有看到您的方法签名,执行此操作的一种方法只是从表单集合中检索
defaultresim
值:
int defaultresim = int.Parse(Request.Form["defaultresim"]);
And then later in your loop: 然后在您的循环中:
image.IsImageDefault = (i == defaultresim);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.