I have an MVC 5 app, and in my _Layout.cshtml file, I have two input fields and a href attribute, and when the user fills them, and presses the href, I want to redirect to a particular view in my app, which contains a form with a few fields, and for the two of the fields, I want to assign the values passed by the input fields in my _Layout.cshtml file, and then let the user fill out the rest of the fields. I have this in my Layout:

<div class="input-top">
     <a class="GoBtn" href=""><img src="~/Content/img/GOBtn.png" class="gobtn-position"></a>
     <input id="homeZipCode" type="text" class="form-control input-position-2" placeholder="ZIP">
     <input id="homeService" type="text" class="form-control input-position-1" placeholder="What do you need done today?">
</div>

Then, I have those two action methods in my controller:

    [Authorize]
    public ActionResult ServiceRequest()
    {
            ViewBag.StateID = new SelectList(db.States, "StateID", "StateName");
            return View();
    }

    [Authorize]
    [HttpPost]
    public ActionResult ServiceRequest(RequestViewModel rvm, HttpPostedFileBase image = null, HttpPostedFileBase video = null)
    {
       ...
    }

So, I don't want to post directly from the AJAX call, but I want to open the view, and assign the values from my input fields in Layout. I guess therefore, I need to call the get method, not the post one. I have something like this in my _Layout.cshtml, but it doesn't seem to work.

<script>

$(document).ready(function () {
    $('a.GoBtn').on('click', function (e) {

        e.preventDefault();

        var homeZipCode = $("#homeZipCode").val();
        var homeService = $("#homeService").val();

        var model = { ZipCode: homeZipCode, ServiceName: homeService };

        $.ajax({
            url: '@Url.Action("ServiceRequest", "Home")',
            contentType: 'application/json; charset=utf-8',
            type: 'GET',
            dataType: 'html',
            data: JSON.stringify(model)
        })
            .success(function (result) {
            });
    });
});

The point is that, when I check the network tab in developer tools, I see that as a response I get my ServiceRequest page, but it shows it as empty, and also my view is not redirected. I just get 200 status but nothing special happens. Below you can find part of my ServiceRequest view:

@using (Html.BeginForm("ServiceRequest", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    @Html.AntiForgeryToken()

                    <div class="form-horizontal">
                        <h4>Request</h4>
                        <hr />

                        <div class="form-group">
                            @Html.LabelFor(model => model.StateID, "State", htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.DropDownList("StateID", null, "Please select a state", htmlAttributes: new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.StateID, "", new { @class = "text-danger" })
                            </div>
                        </div>

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

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

                        // more fields below

Simply, I want after my AJAX call to open that view and for the ZipCode and ServiceName parts, to get the values of the input fields in my Layout. The other fields should be blanks. Any idea how to achieve it?

Related posts

Recent Viewed