Issue with hidden input and very large values in HTML and ASP.NET MVC 3 Razor

后端 未结 6 1986
天涯浪人
天涯浪人 2021-01-19 03:26

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 (

相关标签:
6条回答
  • 2021-01-19 03:51

    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" />
    }
    
    0 讨论(0)
  • 2021-01-19 03:53

    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.

    0 讨论(0)
  • 2021-01-19 04:00

    How about a

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

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

    0 讨论(0)
  • 2021-01-19 04:02

    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.

    0 讨论(0)
  • 2021-01-19 04:02

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题