简体   繁体   中英

Get CheckBox value corresponding to a FileUpload in ASP.NET MVC

I have 3 file uploads and three radio buttons associated with them.
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" />
        &nbsp;<%= Html.RadioButton("defaultresim", "", true, new { id = "defaultresim1" })%><label for="defaultresim1">Set as default</label>
    </p> 
    <p>
        <input type="file" name="resim2" id="resim2" />
        &nbsp;<%= Html.RadioButton("defaultresim", "", true, new { id = "defaultresim2" })%><label for="defaultresim2">Set as default</label>
    </p> 
    <p>
        <input type="file" name="resim3" id="resim3" />
        &nbsp;<%= 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.

I am fairly new to MVC and still hung with WebForms and I cannot find a way out.
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). Then, when you loop through the files, check whether your variable i is the same as the value of the defaultresim form field.

Without seeing your method signature, one way of doing this is just to retrieve the defaultresim value from the form collection:

int defaultresim = int.Parse(Request.Form["defaultresim"]);

And then later in your loop:

image.IsImageDefault = (i == defaultresim);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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