0

I have a Form that creates an entry into my database. I am trying to add an additional form via a modal that will allow the user to populate the first form by searching a property on the webservice.

What I need is for when the user types hits search in the modal it will call an action on controller that then runs my web service client that then returns a list of appropriate information. I then want this list displayed as a table on the page. The user can then select which search result they want to use to which will then populate the original form. I think I can figure most of this out but wasnt sure of the best way to kick off the search from the modal. Code is as follows.

View

@model *******

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#Search">Populate From Service</button>
<!-- Modal -->
<div id="Search" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Search By Property</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <p>blah blah blah</p>
                    <div class="form-group">
                        <label for="API14" class="control-label col-md-2">API</label>
                        <div class ="col-md-10">
                            <input type="text" class="form-control" id="API14" aria-describedby="API14" placeholder="Enter API">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Search" class="btn btn-success" />
                        <input type="button" value="Close" data-dismiss="modal" class="btn btn-danger" />
                    </div>
                </div>

            </div>
        </div>

    </div>
</div>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.API14, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.API14, new { htmlAttributes = new { @class = "form-control"} })
                @Html.ValidationMessageFor(model => model.API14, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PROP_NO, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PROP_NO, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PROP_NO, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PROP_NM, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PROP_NM, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PROP_NM, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ENTITY, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10 ">

                @Html.EditorFor(model => model.ENTITY, new { htmlAttributes = new { @class = "form-control" } } ) 
                @Html.ValidationMessageFor(model => model.ENTITY, "", new { @class = "text-danger" })


            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PROD_ZONE_NM, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PROD_ZONE_NM, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PROD_ZONE_NM, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LEASE_NAME, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LEASE_NAME, new { htmlAttributes = new { @class = "form-control"  } })
                @Html.ValidationMessageFor(model => model.LEASE_NAME, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.WELL_NO, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.WELL_NO, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.WELL_NO, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-success" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
1
  • 1
    I would use AJAX to call MVC and populate my modal. Commented Apr 6, 2017 at 15:12

1 Answer 1

1

You could divide your issue into two steps.

Step 1: "What I need is for when the user types hits search in the modal it will call an action on controller that then runs my web service client that then returns a list of appropriate information. I then want this list displayed as a table on the page."

Add the following to your scripts section:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script>
        $(document).ready(function () {
            $('#submitModal').click(function () {

                var data = {
                    API14: $('#API14').val()
                }
                $.ajax({
                    url: '/Default/SearchByProperty',
                    data: data
                }).success(function (html) {

                    $('#API14List').html(html);
                    $('#Search').modal('toggle');
                });

            });
        }); 

    </script>
}

This is a simple ajax call that takes the value from you modal input and submits to the server. Note the "url" parameter passed in the call as this will need to match your "/{controller}/{action}". Important: You will also need to update your "Search" button#API14 to type "button", not "submit", as this will avoid a postback which is undesired when making AJAX calls.

You can then add something like this to your controller:

public ActionResult SearchByProperty() 
{

    var model = new List<string>();
    model.Add("one");
    model.Add("two");
    model.Add("three");

    return PartialView(model);
}

... which will require the following in a partial view file I named SearchByProperty.cshtml:

@model List<string>

<table>
    @foreach (var item in Model) {
        <tr class="API14-item">
            <td>@item</td>
        </tr>
    }
</table>

Step 2: "The user can then select which search result they want to use to which will then populate the original form. I think I can figure most of this out but wasnt sure of the best way to kick off the search from the modal. Code is as follows."

If you are confident in writing jQuery, then you should be able to write some js in the scripts section of your create page that will take the values you place in your table and populate your form:

$(document).on('click', '.API14-item', function () {
    //
    // TODO: your code here
    //
});

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

For whatever reason, i am unable to successfully register a click event to the `<tr class="API14-item">'.. I am suspecting it may have to do with my html being produced dynamically after the fact. Any thoughts here as to how i can get this to work. I am populating a table inside of a new modal that the user can then select from.
Yes! I updated the code. If you register the click event for the class on the document, rather than the dynamic data as shown with my update, it should persist.

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.