1

I have an index page with a list of "workbooks" titles and for each workbook, there is a "share" button. When pressing the button a bootstrap model (i.e. dialog) appears which displays the title of the workbook and a textarea allowing the user to type in a sharees email addresses.

When the user presses on the "share" button, I am calling a javascript function which calls a controller action that returns a partial view containing the modal dialog with a form inside it. The problem is that after pressing the submit button (i.e. "Share") there are no validation errors being shown to the user and I am not sure why that is. Could anyone provide some ideas, please?

enter image description here

That is my main (index.cshtml) page:

@model DNAAnalysisCore.Models.WorkBookModel
@{
}

@section BodyFill
{
    <script type="text/javascript">

        function showSharingView(title) {
            var url = "@Url.Action("ShowShareDialog", "Home")" + "?workbookTitle=" + encodeURI(title);
            $('#shareFormContainer').load(url,
                function() {
                    $('#shareFormModal').modal("show");
                });
        }

        function hideSharingView() {
            $('#shareFormModal').modal("hide");
        }

    </script>

    <div id="shareFormContainer" >
       <!--PLACEHOLDER FOR SHARE DIALOG -->
    </div>

    <div class="workbook-container">
        <table class="table">
            <tbody>
            @foreach (var workbook in Model.Workbooks)
            {
                <tr>
                    <td>@Html.ActionLink(workbook.Name, "Open", "OpenAnalytics", new {id = Model.Id, workbook = workbook.Name})</td>
                    <td>
                        <button title="Share" class="share-button" onclick='showSharingView("@workbook.Name")'>&nbsp;</button>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    </div>
}

That is my Controller:

public class HomeController : Controller
{
    [HttpGet]
    public IActionResult ShowShareDialog(string workbookTitle)
    {
        var shareModel = new ShareModel
        {
            Title = workbookTitle
        };

        return PartialView("_ShareView", shareModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult ShareWorkbook(string emails, string title)
    {
        var share = new ShareModel
        {
            Emails = emails
        };

        // TODO EMAIL THE SHARED WORKBOOK using the 'title' of the workbook and the 'email' string value

        // return no content to avoid page refresh
        return NoContent();
    }
}

This is my partial view/modal dialog (_ShareView):

@using DNAAnalysisCore.Resources
@model DNAAnalysisCore.Models.ShareModel

<!-- Modal -->
<div class="modal fade" id="shareFormModal" role="dialog">
    <div class="modal-dialog modal-md">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Share Workbook - @Model.Title</h4>

            </div>

            @using (Html.BeginForm("ShareWorkbook", "Home", FormMethod.Post))
            {
            <div class="modal-body">

                <label>@BaseLanguage.Share_workbook_Instruction_text</label>
                <div class="form-group">
                    <textarea class="form-control" asp-for="Emails" rows="4" cols="50" placeholder="@BaseLanguage.ShareDialogPlaceholder"></textarea>
                    @* TODO add client-side validation using jquery.validate.unobtrusive.js. See US268276 *@
                    <span asp-validation-for="Emails" class="text-danger"></span>
                </div>

                <input asp-for="Title" />
            </div>
            <div class="modal-footer">
                <button type="submit" onclick="hideSharingView()" class="btn btn-primary">Share</button>
                <button id="btnCancelDialog" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            </div>
            }

        </div>
    </div>
</div>


@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

This is my ShareModel:

public class ShareModel
{
    [HiddenInput]
    public string Title { get; set; }
    [Required]
    public string Emails { get; set; }
}

1 Answer 1

2

The form is not added to the page when the page loads, the unobtrusive validation will not pick it up.A simple solution is to use $.validator.unobtrusive.parse("#id-of-the-form");.Refer to here.

1.Add id to your form in _ShareView partial view:

@using (Html.BeginForm("ShareWorkbook", "Home", FormMethod.Post,new { @id="partialform"}))

2.Introduce validation file _ValidationScriptsPartial.cshtml into main page(Index.cshtml) and manually register the form with the unobtrusive validation.

@section Scripts
{
@await Html.PartialAsync("_ValidationScriptsPartial")
<script type="text/javascript">

    function showSharingView(title) {
        var url = "@Url.Action("ShowShareDialog", "Home")" + "?workbookTitle=" + encodeURI(title);
        $('#shareFormContainer').load(url,
            function() {
                $('#shareFormModal').modal("show");
                $.validator.unobtrusive.parse("#partialform");
            });
    }
    function hideSharingView() {
        $('#shareFormModal').modal("hide");
    }
</script>
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.