Giter Club home page Giter Club logo

begincollectionitem's People

Contributors

hazzik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

begincollectionitem's Issues

Incorrect behavior of BeginCollectionItem inside Razor's @helper

I have following helper

@helper ColectionItemTemplate(object item)
{
    <div class="collection-item">
        @using (Html.BeginCollectionItem(Html.ViewData.TemplateInfo.HtmlFieldPrefix))
        {
            @Html.EditorFor(_ => item, null, "")
        }
        <br class="clear"/>
        <a href="#" onclick="$(this).parent().remove(); return false;">Remove</a>
    </div>
}

The purpose of this helper is adding collection items on client side by js. I'm using this helper as following

<script type="text/javascript">
(function ($) {
    var encodedTemplate = @(Html.Raw(ColectionItemTemplate(editor).ToString().ToJson()));

    //Rest of the script
})(jQuery);
</script>

First I call .ToString() to convert HelperResult to it's string representation (withot this .ToJson() will render it as empty object, because HelperResult does not have any public properties)
Then I call .ToJson() to get js-friendly string with tags and some entities encoded.
Then I output it as Raw string, because do not want Razor to encode it again.

But I get corrupted output, because BeginCollectionItem writes index field directly to the view and not to helper result.

The output

<script type="text/javascript">
(function ($) {
    var encodedTemplate = <input type="hidden" name="Items.index" autocomplete="off" value="5cd9e232-5e08-46e2-b84b-31ba4106a978" />
"    \u003cdiv class=\"collection-item\"\u003e\n\r\n                \u003cdiv class=\"editor-label\"\u003e\r\n                    \u003clabel for=\"Items_5cd9e232-5e08-46e2-b84b-31ba4106a978__Code\"\u003eC\u003c/label\u003e \r\n                \u003c/div\u003e\r\n            \u003cdiv class=\"editor-field\"\u003e\r\n                \u003cinput class=\"text-box single-line\" data-val=\"true\" data-val-required=\"Code is required\" id=\"Items_5cd9e232-5e08-46e2-b84b-31ba4106a978__Code\" name=\"Items[5cd9e232-5e08-46e2-b84b-31ba4106a978].Code\" type=\"text\" value=\"\" /\u003e  \r\n                \u003cspan class=\"field-validation-valid\" data-valmsg-for=\"Items[5cd9e232-5e08-46e2-b84b-31ba4106a978].Code\" /*rest sciped*/"; 

    //Rest of the script
})(jQuery);
</script>

As you can see I'm getting index before actual of template and it is not encoded.

And even if I will not output the helper to the view I'll get the input tag in place anyway.

<script type="text/javascript">
(function ($) {
    var encodedTemplate = "@{ var x = (Html.Raw(ColectionItemTemplate(editor).ToHtmlString().ToJson())); }";

    //Rest of the script
})(jQuery);
</script>

Output

<script type="text/javascript">
(function ($) {
    var encodedTemplate = "<input type="hidden" name="Items.index" autocomplete="off" value="e0928e85-6d42-4a27-b4b3-baf5071e9467" />
";
    //Rest of the script
})(jQuery);
</script>

The desired output in this case should be var encodedTemplate = ""; but I'm getting the input box, and it is not encoded - error in js.

Cannot exclude using BeginCollectionItem for particular views

I have removed <add namespace="HtmlHelpers.BeginCollectionItem" /> from my web.config
And
@using HtmlHelpers.BeginCollectionItem and @using (Html.BeginCollectionItem){} from my particular views.

Views are still rendering those <ul>, <li> and hidden elements from Collection.cshtml

I want to use BeginCollectionItem only for particular views but it seems to be used everywhere even after I have removed it.

Am I missing something?

BeginCollectionItem breaking publish of MVC5 apps

I'm unsure exactly what caused this, but I noticed that I was unable to publish one of my web apps. This either happened after upgrading to Visual Studio 16.5, or after the most recent Windows Update - all I know is the last time I built the project on March 5th it worked.

The errors were during the aspnet_merge phase of precompiling the views, with the error messages:

error CS0234: The type or namespace name 'WebViewPage<>' does not exist in the namespace 'Sy
stem.Web.Mvc' (are you missing an assembly reference?)

and

error CS0115: '_Page_Views_XXXX_XXXX_cshtml.Execute()': no suitable method found to override

The errors all pointed to something referencing a different version of MVC, and I eventually noticed this project in my NuGet references, which was actually no longer used but had not been removed. Upon removing it, I could publish this again.

I'm mostly posting this here in the hope that there are enough keywords and error messages to save someone else the hours I spent looking for the solution to this yesterday! I no longer use the project but if someone else encounters this and does need it, I suspect the simplest answer would be to recompile it referencing MVC5 instead of MVC2!

Thanks

That's all. Thanks for creating this. Saved me a ton of time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.