Issue with hidden input and very large values in H

2019-02-23 01:14发布

问题:

Since there appears to be a 1024 character limit for hidden input values, what does everyone do for values in excess of this limit? Can a hidden file input (<input type="file" value="some very long value"...) be reasonably used instead? What would the field length restrictions be of any such solution?

<input id="someId" type="hidden" 
value="5538680,5538683,5538858,5539195,5540063,5540812,5540814,5541665,5541666,5541667,
5541668,5541669,5541670,5541671,5541672,5541673,5541674,5541675,5541676,5541677,5541678,
5541679,5541680,5541682,5541683,5541684,5541685,5541686,5541687,5541688,5541689,5541690,
5541691,5541692,5541693,5541694,5541695,5541696,5541697,5541698,5541728,5543254,5543501,
5543502,5543949,5543950,5544073,5544867,5545079,5545642,5545827,5545890,5545891,5545895,
5545896,5546323,5546631,5546632,5546972,5547794,5547900,5547945,5547980,554923...735181,
5735182,5735183,5735184,5735185,5735187,5735188,5735189,5735227,5735228,5735229,5735235,
5735236,5735237,5735238,5735239,5735240,5735241,5735242,5735243,5735273,5735744,5735745,
5735746,5735747,5735748,5735749,5735836,5735837,5735838,5735839,5735840,5735841,5735842,
5735843,5735844,5735845,5735846,5735847,5735848,5735849,5735850,5735851,5735852,5735853,
5735854,5735855,5735856,5735857,5735858,5735859,5737183,5738250,5738563,5738564,5738565,
5738566,5738567,5738568,5738569,5738570,5738731,5738732,5738946" name="someName">

I'm using ASP.NET MVC 3, and would appreciate a solution that could integrate with a minimum of effort. Ideally, I'd like to be able to pass model values in excess of 1024 characters with razor syntax.

I also need to be able to manipulate the value client-side, using JavaScript/jQuery.

What does everyone do to get around this issue? Ideally, I'd like to deal with the value as a single variable. Is there a good way to handle such information? The largest value for my usage appears to be about 40k in size.

Edit: If you note the ... in the value of the input element above, it appears that the value is being shortened to 1024 characters, to fit into the value attribute. I'm not positive of the cause of this issue, but believe it to be a limit on the attribute size. If someone can deny this, and/or explain how to allow for a larger attribute or field somewhere, I would greatly appreciate it. I would prefer keeping the re-factoring to a minimum, as I already have a significant investment in the current architecture. A number of other components currently utilize this list of values in it's current form.

Edit: My mistake! Firebug was reporting the "...", and causing JavaScript errors. It apparently doesn't handle attributes in excess of 1024 characters well. All of the data is in fact populating without issue (when firebug is disabled). This appears to be an issue with firebug. I apologize for the inconvenience.

回答1:

use the grouping

<input name="someIDs[0]" type="hidden" value="5538680"/>
<input name="someIDs[1]" type="hidden" value="5538683/>

update:

controllers

public ActionResult Test()
    {
        Random rand = new Random();
        List<int> list = new List<int>();
        for (int i = 0; i < 10000; i++)
        {
            list.Add(rand.Next(1,999999));
        }
        return View(list);
    }
    [HttpPost]
    public ActionResult Test(int[] item)
    {
        return View(item.ToList());
    }

view

@model List<int>
@{
    ViewBag.Title = "Test";
}

@using (Html.BeginForm())
{
    foreach (int item in Model)
    {

        <input type="hidden" name="item" value="@item" />
    }
    <input type="submit" value="submit" />
}


回答2:

How about a

<textarea name="someName">5538680,5538683,...</textarea>

with a style/css rule of display:none applied to it ?



回答3:

1024 is the limit of any attribute on an HTML element in HTML 3 and below. To get around it, you could use a text area where you put the value in between the tags.

Since you have so much data, you might be better off implementing some sort of paging on the server side and using AJAX to make a request to get the next set of data.



回答4:

I think the most "correct" way to do this would be using data attributes.

#my-info {
    display: none;
}

<div id="my-info" data-ids="id1,id2,etc"></div>

Or through javascript:

<script>
    $("#my-info").data("ids", @Model.JsonIds);
</script>

And have Model.JsonIds populated with a Json string.

I would personally go with the second, probably.



回答5:

I agree with @Gaby about re-thinking the architecture. But if you really must, I suggest you build up javascript array. That way it will be much easier to work with data. it is pretty simple to do this with razor.



回答6:

i don't really think there is a limit. so long as you can get your string length between the " " quotes, and on the same line, it should accept it. there's rumours of it being 65000 characters, however someone wrote a javascript and got it well over 2,000,000 characters.