Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2014
    Location
    Lynnwood, WA
    Posts
    46

    MVC (Soft Deletion for Posts Video): data-slug not working

    I swear I've triple checked my code, but I can't get the javascript to properly auto-fill the slug textbox. Can anyone spot my error? I'm going batty trying to spot what is wrong!

    From Forms.js (other functions deleted for clarity)
    Code:
    $(document)
        .ready(function() {
            $("[data-slug]").each(function() {
                    var $this = $(this);
                    var $sendSlugFrom = $($this.data("slug"));
                    $sendSlugFrom.keyup(function() {
                        var slug = $sendSlugFrom.val();
                        slug = slug.replace(/[^a-zA-Z0-9\s]/g, "");
                        slug = slug.toLowerCase();
                        slug = slug.replace(/\s+/g, "-");
    
                        if (slug.charAt(slug.length - 1) == "-")
                            slug = slug.substr(0, slug.length - 1);
    
                        $this.val(slug);
                    });
                });
    
        });
    Form.cshtml
    Code:
    @model MVCBlog2.Areas.Admin.ViewModels.PostsForm
        
    <h1>@(Model.IsNew ? "Create Post" : " Update Post")</h1>
    
    @using (Html.BeginForm("Form", "Posts"))
    {
        if (!Model.IsNew)
        {
            @Html.HiddenFor(f=>f.PostId)
        }
    
        @Html.AntiForgeryToken()
    
        <div class="row">
            <div class="col-lg-8">
                @Html.ValidationSummary()
    
                <div class="form-group">
                    @Html.LabelFor(f => f.Title)
                    @Html.TextBoxFor(f => f.Title, new {@class = "form-control"})
                </div>
    
                <div class="form-group">
                    @Html.LabelFor(f => f.Slug)
                    @Html.TextBoxFor(f => f.Slug, new { @class = "form-control", data_slug="#Title"})
                </div>
    
                <div class="form-group">
                    @Html.LabelFor(f => f.Content)
                    @Html.TextAreaFor(f => f.Content, new { @class = "form-control" })
                </div>
            </div>
            <div class="col-lg-4">
                <div class="panel panel-info">
                    <div class="panel-heading">
                        Post Actions
                    </div>
                    <div class="panel-body">
                        <input type="submit" value="@(Model.IsNew ? "Publish Post" : "Update Post")" class="btn btn-success btn-sm"/>
                        <a href="@Url.Action("Index")">or cancel</a>
                    </div>
                </div>
            </div>
        </div>
    }

  2. #2
    Join Date
    Feb 2014
    Posts
    272
    A couple of things:
    1. You can syntax check those files looking at my "Doin' the MMO vids again" posts 18 and 19 where I port those files across to the MMO project almost verbatim. I see you have already fixed the bug caused by an update of jquery to v3 so I can't even point that out
    Note: You will see both the forms.js and the forms.cshtml files quoted in full. (make allowances for your namespace being different of course)

    2. Is the script loaded in the page before you hit the create post button on the web page?
    Check your own bundleconfig.cs file (don't compare to mine) to make sure forms.js is loaded into the "~/admin/scripts" bundle. My comments in post 19 are mostly relevant too. You can also check _Layout.cshtml to make sure it renders the "~/admin/scripts" near the bottom.

    Good luck and have fun watching all this js magic work!

  3. #3
    Join Date
    Apr 2017
    Posts
    0

  4. #4
    Join Date
    Jan 2014
    Location
    Lynnwood, WA
    Posts
    46
    The typo is in the javascript - no idea where it is, i still don't see it, but i pasted out just the [data-slug] function from yours and it works fine. whew...

  5. #5
    Join Date
    Dec 2016
    Posts
    0
    I admire programmers, I love it

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •